Skip to content

Commit 0982323

Browse files
committed
Updated to v1.1.0
1 parent 87bf2af commit 0982323

File tree

5 files changed

+357
-47
lines changed

5 files changed

+357
-47
lines changed

README.md

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Google Docs API has been released. When I used this API, I found that it is very
2020
- Delete table, rows and columns of the table.
2121
- New table can be created by including values.
2222
- Append rows to the table by including values.
23+
- Replace texts with images.
24+
25+
- The image data can be retrieved from URL.
26+
- The image data can be uploaded from the local PC.
2327

2428
## Languages
2529

@@ -42,16 +46,18 @@ This library uses [google-api-go-client](https://github.com/googleapis/google-ap
4246

4347
# Method
4448

45-
| Method | Explanation |
46-
| :--------------------------------------------------------------------------- | :---------------------------------------------- |
47-
| [`GetTables()`](#gettables) | Get all tables from Document. |
48-
| [`GetValues()`](#getvalues) | Get values from a table from Document. |
49-
| [`SetValuesBy2DArray(values [][]interface{})`](#setvaluesby2darray) | Set values to a table with 2 dimensional array. |
50-
| [`SetValuesByObject(values []ValueObject)`](#setbaluesbyobject) | Set values to a table with an object. |
51-
| [`DeleteTable()`](#deletetable) | Delete a table. |
52-
| [`DeleteRowsAndColumns(d *DeleteRowsColumnsRequest)`](#deleterowsandcolumns) | Delete rows and columns of a table. |
53-
| [`CreateTable(c *CreateTableRequest)`](#createtable) | Create new table including sell values. |
54-
| [`AppendRow(c *AppendRowRequest)`](#appendrow) | Append row to a table by including values. |
49+
| Method | Explanation |
50+
| :--------------------------------------------------------------------------- | :------------------------------------------------ |
51+
| [`GetTables()`](#gettables) | Get all tables from Document. |
52+
| [`GetValues()`](#getvalues) | Get values from a table from Document. |
53+
| [`SetValuesBy2DArray(values [][]interface{})`](#setvaluesby2darray) | Set values to a table with 2 dimensional array. |
54+
| [`SetValuesByObject(values []ValueObject)`](#setbaluesbyobject) | Set values to a table with an object. |
55+
| [`DeleteTable()`](#deletetable) | Delete a table. |
56+
| [`DeleteRowsAndColumns(d *DeleteRowsColumnsRequest)`](#deleterowsandcolumns) | Delete rows and columns of a table. |
57+
| [`CreateTable(c *CreateTableRequest)`](#createtable) | Create new table including sell values. |
58+
| [`AppendRow(c *AppendRowRequest)`](#appendrow) | Append row to a table by including values. |
59+
| [`ReplaceTextsToImagesByURL(from, to string)`](#replacetexts) | Replace texts with images from URL. |
60+
| [`ReplaceTextsToImagesByFile(from, to string)`](#replacetexts) | Replace texts with images from files on local PC. |
5561

5662
This library uses [google-api-go-client](https://github.com/googleapis/google-api-go-client).
5763

@@ -78,7 +84,7 @@ About the authorization, please check the section of [Authorization](#authorizat
7884

7985
## Scope
8086

81-
In this library, using the scope of `https://www.googleapis.com/auth/documents` is recommended.
87+
In this library, using the scope of `https://www.googleapis.com/auth/documents` is recommended. When the method of `ReplaceTextsToImagesByFile` is used, also please add `https://www.googleapis.com/auth/drive`.
8288

8389
<a name="gettables"></a>
8490

@@ -355,10 +361,85 @@ When above script is run, the following result is obtained. In this case, the va
355361

356362
![](images/fig5.png)
357363

358-
#### From:
364+
#### To:
359365

360366
![](images/fig6.png)
361367

368+
<a name="replacetexts"></a>
369+
370+
## 9. ReplaceTextsToImagesByURL and ReplaceTextsToImagesByFile
371+
372+
### Sample script 1
373+
374+
In this sample, the texts `{{sample}}` in all tables are replaced with the image retrieved by **the URL of `https://###/sample.png`**.
375+
376+
```golang
377+
documentID := "###"
378+
searchText := "{{sample}}"
379+
tableOnly := true
380+
replaceImageURL := "https://###/sample.png"
381+
g := gdoctableapp.New()
382+
383+
res, err := g.Docs(documentID).ReplaceTextsToImagesByURL(searchText, replaceImageURL).TableOnly(tableOnly).Do(client)
384+
```
385+
386+
### Sample script 2
387+
388+
In this sample, the texts `{{sample}}` in all tables are replaced with the image retrieved by **the file of `./sample.png` on your local PC**.
389+
390+
```golang
391+
documentID := "###"
392+
searchText := "{{sample}}"
393+
tableOnly := true // default is false
394+
replaceImageFilePath := "./sample.png"
395+
g := gdoctableapp.New()
396+
397+
res, err := g.Docs(documentID).ReplaceTextsToImagesByFile(searchText, replaceImageFilePath).TableOnly(tableOnly).Do(client)
398+
```
399+
400+
- `documentID`: Document ID.
401+
- `client`: `*Client` for using Docs API. Please check the section of [Authorization](#authorization).
402+
- `searchText`: Search text. This text is replaced with image.
403+
- `tableOnly`: When this is `true`, only texts in the table are replaced with image. When this is `false`, the texts in the body are replaced.
404+
- `replaceImageURL`: URL of the image.
405+
- `replaceImageFilePath`: File path of the image.
406+
407+
If you want to change the width and height of the image, please use the method of `SetImageSize(width, height float64)` like below.
408+
409+
```golang
410+
res, err := g.Docs(documentID).SetImageSize(100, 100).ReplaceTextsToImagesByFile(searchText, replaceImageFilePath).TableOnly(tableOnly).Do(client)
411+
```
412+
413+
### Note
414+
415+
- The flow for replacing the text with the image on the local PC.
416+
417+
1. Upload the image from local PC to Google Drive.
418+
2. Publicly share the image file. - The time for sharing is several seconds. The file is delete after the image is put.
419+
3. Put the image using the URL of the publicly shared file.
420+
4. Delete the image. - Even when the image is delete from Google Drive, the put image on Google Document is not deleted.
421+
422+
- About `SetImageSize`
423+
> [**objectSize**](https://developers.google.com/docs/api/reference/rest/v1/documents/request#insertinlineimagerequest): The size that the image should appear as in the document. This property is optional and the final size of the image in the document is determined by the following rules: _ If neither width nor height is specified, then a default size of the image is calculated based on its resolution. _ If one dimension is specified then the other dimension is calculated to preserve the aspect ratio of the image. \* If both width and height are specified, the image is scaled to fit within the provided dimensions while maintaining its aspect ratio.
424+
425+
### Result
426+
427+
When above script is run, the following result is obtained.
428+
429+
#### From:
430+
431+
![](images/fig7.png)
432+
433+
#### To:
434+
435+
![](images/fig8.png)
436+
437+
The image of `https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png` was used as the sample image.
438+
439+
When `tableOnly` is `false`, the following result is retrieved.
440+
441+
![](images/fig9.png)
442+
362443
<a name="authorization"></a>
363444

364445
# Authorization
@@ -535,6 +616,7 @@ func main() {
535616
```
536617

537618
# Sample scripts
619+
538620
- [Creating a Table to Google Document by Retrieving Values from Google Spreadsheet for Golang](https://gist.github.com/tanaikech/0589a673cae9569181def8ccd10793cf)
539621

540622
# Limitations
@@ -574,4 +656,8 @@ If you have any questions and commissions for me, feel free to tell me.
574656

575657
1. When the inline objects and tables are put in the table. An error occurred. This bug was removed by this update.
576658

659+
- v1.1.0 (January 22, 2020)
660+
661+
1. [2 new methods were added.](#replacetexts) From this version, the texts can be replaced by images. The direct link and local file can be used as the image.
662+
577663
[TOP](#top)

handler.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
// This file includes handler method.
44
package gdoctableapp
55

6+
// checkOutputValues : Check output values.
7+
func (o *obj) checkOutputValues() {
8+
if !o.params.ShowAPIResponseFlag {
9+
o.result.ResponseFromAPIs = nil
10+
}
11+
}
12+
13+
// handler : Handler of gdoctableapp
614
func (o *obj) handler() (*Result, error) {
715
if err := o.optionChecker(); err != nil {
816
return nil, err
@@ -23,10 +31,7 @@ func (o *obj) handler() (*Result, error) {
2331

2432
// getTables
2533
if o.params.Works.DoGetTables {
26-
o.getTables()
27-
if !o.params.ShowAPIResponseFlag {
28-
o.result.ResponseFromAPIs = nil
29-
}
34+
o.getTables().checkOutputValues()
3035
return &o.result, nil
3136
}
3237

@@ -37,9 +42,7 @@ func (o *obj) handler() (*Result, error) {
3742
return nil, err
3843
}
3944
o.result.Values = values
40-
if !o.params.ShowAPIResponseFlag {
41-
o.result.ResponseFromAPIs = nil
42-
}
45+
o.checkOutputValues()
4346
return &o.result, nil
4447
}
4548

@@ -48,9 +51,7 @@ func (o *obj) handler() (*Result, error) {
4851
if err := o.setValues(); err != nil {
4952
return nil, err
5053
}
51-
if !o.params.ShowAPIResponseFlag {
52-
o.result.ResponseFromAPIs = nil
53-
}
54+
o.checkOutputValues()
5455
return &o.result, nil
5556
}
5657

@@ -59,9 +60,7 @@ func (o *obj) handler() (*Result, error) {
5960
if err := o.deleteTable(); err != nil {
6061
return nil, err
6162
}
62-
if !o.params.ShowAPIResponseFlag {
63-
o.result.ResponseFromAPIs = nil
64-
}
63+
o.checkOutputValues()
6564
return &o.result, nil
6665
}
6766

@@ -70,9 +69,7 @@ func (o *obj) handler() (*Result, error) {
7069
if err := o.deleteRowsColumns(); err != nil {
7170
return nil, err
7271
}
73-
if !o.params.ShowAPIResponseFlag {
74-
o.result.ResponseFromAPIs = nil
75-
}
72+
o.checkOutputValues()
7673
return &o.result, nil
7774
}
7875

@@ -81,9 +78,7 @@ func (o *obj) handler() (*Result, error) {
8178
if err := o.crateTable(); err != nil {
8279
return nil, err
8380
}
84-
if !o.params.ShowAPIResponseFlag {
85-
o.result.ResponseFromAPIs = nil
86-
}
81+
o.checkOutputValues()
8782
return &o.result, nil
8883
}
8984

@@ -92,9 +87,16 @@ func (o *obj) handler() (*Result, error) {
9287
if err := o.appendRow(); err != nil {
9388
return nil, err
9489
}
95-
if !o.params.ShowAPIResponseFlag {
96-
o.result.ResponseFromAPIs = nil
90+
o.checkOutputValues()
91+
return &o.result, nil
92+
}
93+
94+
// replaceTextsToImages
95+
if o.params.Works.DoReplaceTextsToImagesByURL || o.params.Works.DoReplaceTextsToImagesByFile {
96+
if err := o.replaceTextsToImages(); err != nil {
97+
return nil, err
9798
}
99+
o.checkOutputValues()
98100
return &o.result, nil
99101
}
100102

methods.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,53 @@ import (
1313
/// Methods
1414
///
1515

16+
// ReplaceTextsToImagesByURL : Replace texts to images in tables by an image URL.
17+
//
18+
// from: Search text
19+
//
20+
// to: URL of image for replacing the searched texts
21+
//
22+
// tableOnly: When you want to replace the texts in only table cells, please set true. When you set false, the text is searched from all body and replaced to images.
23+
//
24+
// sample:
25+
// var g *gdoctableapp.Params
26+
// searchText := "sample"
27+
// replaceImageURL := "https://sample/sample.png"
28+
// tableOnly := true
29+
// res, err := g.Docs(documentID).ReplaceTextsToImages(searchText, replaceImageURL, tableOnly).Do(client)
30+
//
31+
func (p *Params) ReplaceTextsToImagesByURL(from, to string) *Params {
32+
p.Works.DoReplaceTextsToImagesByURL = true
33+
p.ReplaceTextsToImagesP.ReplaceFromText = from
34+
p.ReplaceTextsToImagesP.ReplaceToImage = to
35+
return p
36+
}
37+
38+
// ReplaceTextsToImagesByFile : Replace texts to images in tables by an image file.
39+
func (p *Params) ReplaceTextsToImagesByFile(from, to string) *Params {
40+
p.Works.DoReplaceTextsToImagesByFile = true
41+
p.ReplaceTextsToImagesP.ReplaceFromText = from
42+
p.ReplaceTextsToImagesP.ReplaceToImage = to
43+
return p
44+
}
45+
46+
// TableOnly : Whether searches only the tables.
47+
func (p *Params) TableOnly(tableOnly bool) *Params {
48+
if tableOnly {
49+
p.ReplaceTextsToImagesP.ReplaceTableOnly = true
50+
} else {
51+
p.ReplaceTextsToImagesP.ReplaceTableOnly = false
52+
}
53+
return p
54+
}
55+
56+
// SetImageSize : Set image size.
57+
func (p *Params) SetImageSize(width, height float64) *Params {
58+
p.ReplaceTextsToImagesP.Width = width
59+
p.ReplaceTextsToImagesP.Height = height
60+
return p
61+
}
62+
1663
// AppendRow : Append rows and values to existing table.
1764
func (p *Params) AppendRow(c *AppendRowRequest) *Params {
1865
p.Works.DoAppendRow = true
@@ -102,6 +149,7 @@ func (o *obj) init() error {
102149
func (p *Params) Do(client *http.Client) (*Result, error) {
103150
o := &obj{
104151
params: *p,
152+
fields: defaultFields,
105153
}
106154
o.params.Client = client
107155
if err := o.init(); err != nil {

0 commit comments

Comments
 (0)