/
processes.go
798 lines (765 loc) · 22 KB
/
processes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Package gdoctableapp (go-gdoctableapp.go) :
// This is a Golang library for managing tables in Google Document using Google Docs API.
package gdoctableapp
import (
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
docs "google.golang.org/api/docs/v1"
drive "google.golang.org/api/drive/v3"
)
// getTables : Retrieve all tables.
func (o *obj) getTables() *obj {
for i, table := range o.docTables {
o.docTable = table
o.parseTable()
res := [][]string{}
for _, e := range o.contents {
temp1 := []string{}
for _, f := range e {
temp2 := []string{}
for _, g := range f.tempColsContent {
temp2 = append(temp2, strings.Replace(g.content, "\n", "", -1))
}
temp1 = append(temp1, strings.Join(temp2, "\n"))
}
res = append(res, temp1)
}
t := &Table{}
t.Index = int64(i)
t.Values = res
t.TablePosition.StartIndex = table.StartIndex
t.TablePosition.EndIndex = table.EndIndex
o.result.Tables = append(o.result.Tables, *t)
}
return o
}
// getValues : Retrieve values from a table of Document.
func (o *obj) getValues() ([][]string, error) {
o.parseTable()
res := [][]string{}
for _, e := range o.contents {
temp1 := []string{}
for _, f := range e {
temp2 := []string{}
for _, g := range f.tempColsContent {
temp2 = append(temp2, strings.Replace(g.content, "\n", "", -1))
}
temp1 = append(temp1, strings.Join(temp2, "\n"))
}
res = append(res, temp1)
}
return res, nil
}
// createInsertInlineImageRequest : Create InsertInlineImageRequest.
func createInsertInlineImageRequest(startIndex int64, url string, width, height float64) *docs.Request {
r := &docs.InsertInlineImageRequest{
Uri: url,
Location: &docs.Location{
Index: startIndex,
},
}
if width > 0 && height > 0 {
r.ObjectSize = &docs.Size{
Width: &docs.Dimension{
Unit: "PT",
Magnitude: width,
},
Height: &docs.Dimension{
Unit: "PT",
Magnitude: height,
},
}
}
req := &docs.Request{
InsertInlineImage: r,
}
return req
}
// createDeleteContentRangeRequest : Create DeleteContentRangeRequest.
func createDeleteContentRangeRequest(startIndex, endIndex int64) *docs.Request {
r := &docs.DeleteContentRangeRequest{}
r.Range = &docs.Range{
StartIndex: startIndex,
EndIndex: endIndex,
}
req := &docs.Request{
DeleteContentRange: r,
}
return req
}
// deleteTable : Delete table.
func (o *obj) deleteTable() error {
o.parseTable()
dr := createDeleteContentRangeRequest(o.docTable.StartIndex, o.docTable.EndIndex)
br := &docs.BatchUpdateDocumentRequest{}
br.Requests = append(br.Requests, dr)
o.requestBody = br
if err := o.documentbatchUpdate(); err != nil {
return err
}
return nil
}
// deleteRowsColumns : Delete rows and columns of a table.
func (o *obj) deleteRowsColumns() error {
if len(o.params.DeleteRowsColumnsRequest.Rows) == 0 && len(o.params.DeleteRowsColumnsRequest.Columns) == 0 {
return fmt.Errorf("No parameters for using DeleteRowsAndColumns()")
}
rows := o.params.DeleteRowsColumnsRequest.Rows
cols := o.params.DeleteRowsColumnsRequest.Columns
sort.Slice(rows, func(i, j int) bool { return rows[i] > rows[j] })
sort.Slice(cols, func(i, j int) bool { return cols[i] > cols[j] })
maxDeleteRow := rows[0]
maxDeleteCol := cols[0]
table := o.docTable.Table
if table.Rows < maxDeleteRow || table.Columns < maxDeleteCol {
return fmt.Errorf("Rows and columns for deleting are outside of the table")
}
inputObj := o.params.DeleteRowsColumnsRequest
l := &docs.Location{}
l.Index = o.docTable.StartIndex
br := &docs.BatchUpdateDocumentRequest{}
if len(inputObj.Rows) > 0 {
for _, e := range inputObj.Rows {
tc := &docs.TableCellLocation{}
tc.TableStartLocation = l
tc.RowIndex = e
r := &docs.DeleteTableRowRequest{}
r.TableCellLocation = tc
dr := &docs.Request{
DeleteTableRow: r,
}
br.Requests = append(br.Requests, dr)
}
}
if len(inputObj.Columns) > 0 {
for _, e := range inputObj.Columns {
tc := &docs.TableCellLocation{}
tc.TableStartLocation = l
tc.ColumnIndex = e
r := &docs.DeleteTableColumnRequest{}
r.TableCellLocation = tc
dr := &docs.Request{
DeleteTableColumn: r,
}
br.Requests = append(br.Requests, dr)
}
}
o.requestBody = br
if err := o.documentbatchUpdate(); err != nil {
return err
}
return nil
}
// setValuesMain : Main method for setValues.
func (o *obj) setValuesMain() error {
dupChk, err := o.checkDupValues()
if err != nil {
return err
}
if len(dupChk.dup) > 0 {
return fmt.Errorf("Range of inputted values are duplicated")
}
o.parseInputValuesForSetValues(dupChk)
o.addRowsAndColumnsForSetValues()
if o.requestBody != nil {
if err := o.documentbatchUpdate(); err != nil {
return err
}
if err := o.getTable(); err != nil {
return err
}
}
o.parseTable()
o.createSetValuesRequests()
if err := o.documentbatchUpdate(); err != nil {
return err
}
return nil
}
// setValues : Set values.
func (o *obj) setValues() error {
if o.params.Works.DoValuesArray {
vo := &ValueObject{}
vo.Values = o.params.ValuesArray
vo.Range.StartRowIndex = 0
vo.Range.StartColumnIndex = 0
o.params.ValuesObject = append(o.params.ValuesObject, *vo)
}
if err := o.setValuesMain(); err != nil {
return err
}
return nil
}
// createInsertTableRowColumnRequestBody : Create requests body of insertTableRow and insertTableColumn.
func (o *obj) createInsertTableRowColumnRequestBody(maxRow, maxCol int64) *docs.BatchUpdateDocumentRequest {
startIndex := o.docTable.StartIndex
tableRow := o.docTable.Table.Rows
tableCol := o.docTable.Table.Columns
addRows := maxRow - tableRow
addColumns := maxCol - tableCol
br := &docs.BatchUpdateDocumentRequest{}
if addRows > 0 {
for i := int64(0); i < addRows; i++ {
l := &docs.Location{}
l.Index = startIndex
tc := &docs.TableCellLocation{}
tc.RowIndex = tableRow - 1 + i
tc.TableStartLocation = l
t := &docs.InsertTableRowRequest{}
t.InsertBelow = true
t.TableCellLocation = tc
dr := &docs.Request{
InsertTableRow: t,
}
br.Requests = append(br.Requests, dr)
}
}
if addColumns > 0 {
for i := int64(0); i < addColumns; i++ {
l := &docs.Location{}
l.Index = startIndex
tc := &docs.TableCellLocation{}
tc.ColumnIndex = tableCol - 1 + i
tc.TableStartLocation = l
t := &docs.InsertTableColumnRequest{}
t.InsertRight = true
t.TableCellLocation = tc
dr := &docs.Request{
InsertTableColumn: t,
}
br.Requests = append(br.Requests, dr)
}
}
return br
}
// createInsertTableRowColumnRequest : Create requests of insertTableRow and insertTableColumn.
func (o *obj) createInsertTableRowColumnRequest(maxRow, maxCol int64) {
br := o.createInsertTableRowColumnRequestBody(maxRow, maxCol)
if len(br.Requests) > 0 {
o.requestBody = br
}
}
// createSetValuesRequests : Create the requests for putting values.
func (o *obj) createSetValuesRequests() {
br := &docs.BatchUpdateDocumentRequest{}
for i := int64(len(o.parsedValues)) - 1; i >= 0; i-- {
r := o.parsedValues[i].row
c := o.parsedValues[i].col
v := o.parsedValues[i].content
delReq := o.delCell[r][c]
if delReq.DeleteContentRange.Range.StartIndex != delReq.DeleteContentRange.Range.EndIndex {
br.Requests = append(br.Requests, delReq)
}
if v != "" {
t := &docs.InsertTextRequest{}
location := &docs.Location{}
location.Index = delReq.DeleteContentRange.Range.StartIndex
t.Location = location
t.Text = v
dr := &docs.Request{
InsertText: t,
}
br.Requests = append(br.Requests, dr)
}
}
o.requestBody = br
}
// createTable : Create new table with values.
func (o *obj) crateTable() error {
br := &docs.BatchUpdateDocumentRequest{}
table := &docs.InsertTableRequest{}
rows := o.params.CreateTableRequest.Rows
columns := o.params.CreateTableRequest.Columns
if rows == 0 || columns == 0 {
return fmt.Errorf("Values of Rows and/or Columns are not found")
}
table.Rows = rows
table.Columns = columns
var idx int64
if o.params.CreateTableRequest.Append {
el := &docs.EndOfSegmentLocation{
SegmentId: "",
}
table.EndOfSegmentLocation = el
dr1 := &docs.Request{
InsertTable: table,
}
br.Requests = append(br.Requests, dr1)
o.requestBody = br
if err := o.documentbatchUpdate(); err != nil {
return err
}
br = &docs.BatchUpdateDocumentRequest{} // Reset
contents, err := o.getDocument()
if err != nil {
return err
}
for i := len(contents) - 1; i >= 0; i-- {
if table := contents[i].Table; table != nil {
o.docTable = contents[i]
break
}
}
idx = o.docTable.StartIndex - 1
} else if o.params.CreateTableRequest.Index != 0 {
loc := &docs.Location{}
loc.Index = o.params.CreateTableRequest.Index
table.Location = loc
dr1 := &docs.Request{
InsertTable: table,
}
br.Requests = append(br.Requests, dr1)
idx = o.params.CreateTableRequest.Index
} else {
return fmt.Errorf("Please set Index (> 0) or Append")
}
if len(o.params.CreateTableRequest.Values) > 0 {
val, err := parseInputValues(
o.params.CreateTableRequest.Values,
idx,
o.params.CreateTableRequest.Rows,
o.params.CreateTableRequest.Columns,
)
if err != nil {
return err
}
for i := int64(len(val)) - 1; i >= 0; i-- {
v := val[i].content
if v != "" {
t := &docs.InsertTextRequest{
Location: &docs.Location{
Index: val[i].index,
},
Text: v,
}
dr2 := &docs.Request{
InsertText: t,
}
br.Requests = append(br.Requests, dr2)
}
}
}
if o.params.CreateTableRequest.Index > 0 || len(o.params.CreateTableRequest.Values) > 0 {
o.requestBody = br
if err := o.documentbatchUpdate(); err != nil {
return err
}
}
return nil
}
// appendRow : Append row with values.
func (o *obj) appendRow() error {
if len(o.params.AppendRowRequest.Values) == 0 {
return fmt.Errorf("Values for putting are not set")
}
vo := &ValueObject{}
vo.Values = o.params.AppendRowRequest.Values
vo.Range.StartRowIndex = o.docTable.Table.Rows
vo.Range.StartColumnIndex = 0
o.params.ValuesObject = append(o.params.ValuesObject, *vo)
if err := o.setValuesMain(); err != nil {
return err
}
return nil
}
// appendBrForInsertInlineImage : Append request to slice.
func (o *obj) appendBrForInsertInlineImage(br *docs.BatchUpdateDocumentRequest, startIndex, endIndex int64) {
br.Requests = append(br.Requests, createDeleteContentRangeRequest(startIndex, endIndex))
br.Requests = append(br.Requests, createInsertInlineImageRequest(
startIndex,
o.params.ReplaceTextsToImagesP.ReplaceToImage,
o.params.ReplaceTextsToImagesP.Width,
o.params.ReplaceTextsToImagesP.Height,
))
}
// getTextRunContent : Get textrun content.
func (o *obj) getTextRunContent(ar *[]docs.ParagraphElement, h *docs.StructuralElement) {
if h.Paragraph != nil {
for _, e := range h.Paragraph.Elements {
if e.TextRun != nil {
if strings.Contains(e.TextRun.Content, o.params.ReplaceTextsToImagesP.ReplaceFromText) {
*ar = append(*ar, *e)
}
}
}
}
}
// getTableContent : Get table content.
func (o *obj) getTableContent(e *docs.StructuralElement) []docs.ParagraphElement {
var ar []docs.ParagraphElement
for _, f := range e.Table.TableRows {
for _, g := range f.TableCells {
for _, h := range g.Content {
o.getTextRunContent(&ar, h)
}
}
}
return ar
}
// uploadImageFile : Upload image file.
func (o *obj) uploadImageFile() error {
imgFile, err := os.Open(o.params.ReplaceTextsToImagesP.ReplaceToImage)
if err != nil {
return err
}
defer imgFile.Close()
if err := o.getSrvForDrive(); err != nil {
return err
}
f := &drive.File{
Name: filepath.Base(o.params.ReplaceTextsToImagesP.ReplaceToImage) + "_From_gdoctableapp",
}
file, err := o.srvDrive.Files.Create(f).Media(imgFile).Fields("id,webContentLink").Do()
if err != nil {
return err
}
o.result.ResponseFromAPIs = append(o.result.ResponseFromAPIs, file)
o.params.ReplaceTextsToImagesP.FileID = file.Id
o.params.ReplaceTextsToImagesP.ReplaceToImage = file.WebContentLink
permissiondata := &drive.Permission{
Type: "anyone",
Role: "reader",
}
resPermissions, err := o.srvDrive.Permissions.Create(file.Id, permissiondata).Do()
if err != nil {
return err
}
o.result.ResponseFromAPIs = append(o.result.ResponseFromAPIs, resPermissions)
return nil
}
// getSrvForDrive : Get service for using Drive API.
func (o *obj) getSrvForDrive() error {
srv, err := drive.New(o.params.Client)
if err != nil {
return err
}
o.srvDrive = srv
return nil
}
// replaceTextsToImages : Replace texts to images by URL.
func (o *obj) replaceTextsToImages() error {
if o.params.Works.DoReplaceTextsToImagesByFile {
if err := o.uploadImageFile(); err != nil {
return err
}
}
o.fields = "body(content)"
contents, err := o.getDocument()
if err != nil {
return err
}
var ar []docs.ParagraphElement
for _, e := range contents {
if e.Table != nil {
ar = append(ar, o.getTableContent(e)...)
} else if !o.params.ReplaceTextsToImagesP.ReplaceTableOnly && e.Paragraph != nil {
o.getTextRunContent(&ar, e)
}
}
br := &docs.BatchUpdateDocumentRequest{}
for i := int64(len(ar)) - 1; i >= 0; i-- {
e := ar[i]
content := e.TextRun.Content
searchText := o.params.ReplaceTextsToImagesP.ReplaceFromText
if strings.TrimSpace(content) == searchText {
offset := len(content) - len(strings.TrimSpace(content))
o.appendBrForInsertInlineImage(br, e.StartIndex, e.EndIndex-int64(offset))
} else {
start := e.StartIndex + int64(strings.Index(content, searchText))
o.appendBrForInsertInlineImage(br, start, start+int64(len(searchText)))
}
}
if len(br.Requests) > 0 {
o.requestBody = br
if err := o.documentbatchUpdate(); err != nil {
return err
}
if o.params.Works.DoReplaceTextsToImagesByFile {
err := o.srvDrive.Files.Delete(o.params.ReplaceTextsToImagesP.FileID).Do()
if err != nil {
return err
}
}
} else {
o.result.Message = fmt.Sprintf("'%s' was not found.", o.params.ReplaceTextsToImagesP.ReplaceFromText)
}
return nil
}
// convertStr : Convert value to string.
func convertStr(v interface{}) (string, error) {
switch value := v.(type) {
case int:
if g, ok := v.(int); ok {
return fmt.Sprint(g), nil
}
return "", fmt.Errorf("error")
case int64:
if g, ok := v.(int64); ok {
return fmt.Sprint(g), nil
}
return "", fmt.Errorf("error")
case float64:
if g, ok := v.(float64); ok {
return fmt.Sprint(g), nil
}
return "", fmt.Errorf("error")
case string:
if g, ok := v.(string); ok {
return g, nil
}
return "", fmt.Errorf("error")
default:
return "", fmt.Errorf("error: Unknown value: %+v, %+v", v, value)
}
}
// parseInputValues : Parse input values for 2 dimensional array.
func parseInputValues(values [][]interface{}, index, rows, cols int64) ([]tempCheckDupValues, error) {
index += 4
v := []tempCheckDupValues{}
var maxCol int64
maxRow := int64(len(values))
for row := int64(0); row < rows; row++ {
if maxRow > row {
maxCol = int64(len(values[row]))
} else {
maxCol = cols
}
for col := int64(0); col < cols; col++ {
if maxRow > row && maxCol > col && values[row][col] != "" {
colVal, err := convertStr(values[row][col])
if err != nil {
return nil, err
}
temp := &tempCheckDupValues{
row: int64(row),
col: int64(col),
content: colVal,
index: index,
}
v = append(v, *temp)
}
index += 2
}
index++
}
return v, nil
}
// addRowsAndColumnsForSetValues : Create requests for adding rows and columns for the inputted values.
func (o *obj) addRowsAndColumnsForSetValues() {
values := o.params.ValuesObject
var maxRow, maxCol int64
for _, e := range values {
tMaxRow := int64(len(e.Values)) + e.Range.StartRowIndex
tMaxCol := func() int64 {
var n int64
for _, f := range e.Values {
if n < int64(len(f)) {
n = int64(len(f))
}
}
return n
}() + e.Range.StartColumnIndex
if maxRow < tMaxRow {
maxRow = tMaxRow
}
if maxCol < tMaxCol {
maxCol = tMaxCol
}
}
o.createInsertTableRowColumnRequest(maxRow, maxCol)
}
// parseInputValuesForSetValues : Sort the inputted values.
func (o *obj) parseInputValuesForSetValues(dupChk *dupCheck) {
sort.Slice(dupChk.noDup, func(i, j int) bool {
if dupChk.noDup[i].row < dupChk.noDup[j].row {
return true
} else if (dupChk.noDup[i].col < dupChk.noDup[j].col) && (dupChk.noDup[i].row == dupChk.noDup[j].row) {
return true
}
return false
})
o.parsedValues = dupChk.noDup
}
// checkDupValues : Check the duplication of values.
func (o *obj) checkDupValues() (*dupCheck, error) {
values := o.params.ValuesObject
temp := []tempCheckDupValues{}
for _, e := range values {
rowOffset := e.Range.StartRowIndex
colOffset := e.Range.StartColumnIndex
for i, row := range e.Values {
temp2 := []tempCheckDupValues{}
for j, col := range row {
colVal, err := convertStr(col)
if err != nil {
return nil, err
}
t := &tempCheckDupValues{
row: int64(i) + rowOffset,
col: int64(j) + colOffset,
content: colVal,
}
temp2 = append(temp2, *t)
}
temp = append(temp, temp2...)
}
}
dc := &dupCheck{}
for _, e := range temp {
c := false
for _, f := range dc.noDup {
if f.row == e.row && f.col == e.col {
c = true
}
}
if c {
dc.dup = append(dc.dup, e)
} else {
dc.noDup = append(dc.noDup, e)
}
}
return dc, nil
}
// parseTable : Parse the retrieved table.
func (o *obj) parseTable() {
docContent := o.docTable
tableRows := docContent.Table.TableRows
var rowsDelCell [][]*docs.Request
var rowsContents [][]*tempColsContents
for i := 0; i < len(tableRows); i++ {
tableCells := tableRows[i].TableCells
var tRowsDelCell []*docs.Request
var tRowsContents []*tempColsContents
for j := 0; j < len(tableCells); j++ {
tColsContents := &tempColsContents{}
contents := tableCells[j].Content
var si int64
var ei int64
for k := 0; k < len(contents); k++ {
if contents[k].Paragraph != nil {
elements := contents[k].Paragraph.Elements
for l := 0; l < len(elements); l++ {
if k == 0 && l == 0 {
si = elements[l].StartIndex
}
if k == len(contents)-1 && l == len(elements)-1 {
ei = elements[l].EndIndex - 1
}
cellContent := ""
if elements[l].TextRun != nil {
cellContent = elements[l].TextRun.Content
} else if elements[l].InlineObjectElement != nil {
cellContent = "[INLINE OBJECT]"
} else {
cellContent = "[UNSUPPORTED CONTENT]"
}
tColsContent := &tempColsContent{
startIndex: elements[l].StartIndex,
endIndex: elements[l].EndIndex,
content: cellContent, // At Docs API, content is automatically converted to string.
}
tColsContents.tempColsContent = append(tColsContents.tempColsContent, *tColsContent)
}
} else if contents[k].Table != nil {
tColsContent := &tempColsContent{
startIndex: contents[k].StartIndex,
endIndex: contents[k].EndIndex,
content: "[TABLE]",
}
tColsContents.tempColsContent = append(tColsContents.tempColsContent, *tColsContent)
} else {
tColsContent := &tempColsContent{
startIndex: contents[k].StartIndex,
endIndex: contents[k].EndIndex,
content: "[UNSUPPORTED CONTENT]",
}
tColsContents.tempColsContent = append(tColsContents.tempColsContent, *tColsContent)
}
}
tRowsDelCell = append(tRowsDelCell, createDeleteContentRangeRequest(si, ei))
tRowsContents = append(tRowsContents, tColsContents)
}
rowsDelCell = append(rowsDelCell, tRowsDelCell)
rowsContents = append(rowsContents, tRowsContents)
}
o.delCell = rowsDelCell
o.contents = rowsContents
o.cell1stIndex = rowsContents[0][0].tempColsContent[0].startIndex
}
// getAllTables : Retrieve all tables from Google Document.
func (o *obj) getAllTables() error {
contents, err := o.getDocument()
if err != nil {
return err
}
for _, e := range contents {
if table := e.Table; table != nil {
o.docTables = append(o.docTables, e)
}
}
return nil
}
// getTable : Retrieve table from Google Document.
func (o *obj) getTable() error {
contents, err := o.getDocument()
if err != nil {
return err
}
c := 0
for _, e := range contents {
if table := e.Table; table != nil {
if o.params.TableIdx == c {
o.docTable = e
break
}
c++
}
}
if o.docTable == nil {
return fmt.Errorf("Table of index of %d was not found", o.params.TableIdx)
}
return nil
}
// documentbatchUpdate : Request the method of batchUpdate for Google Document.
func (o *obj) documentbatchUpdate() error {
if o.requestBody != nil {
doc, err := o.srv.Documents.BatchUpdate(o.params.DocumentID, o.requestBody).Do()
if err != nil {
return err
}
o.result.ResponseFromAPIs = append(o.result.ResponseFromAPIs, doc)
o.requestBody = nil
}
return nil
}
// getDocument : Retrieve Document object from Google Document.
func (o *obj) getDocument() ([]*docs.StructuralElement, error) {
doc, err := o.srv.Documents.Get(o.params.DocumentID).Fields(o.fields).Do()
if err != nil {
return nil, err
}
o.result.ResponseFromAPIs = append(o.result.ResponseFromAPIs, doc)
return doc.Body.Content, nil
}
// optionChecker : Check inputted options.
func (o *obj) optionChecker() error {
r := reflect.ValueOf(&o.params.Works).Elem()
rt := r.Type()
fl := 0
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
rv := reflect.ValueOf(&o.params.Works)
value := reflect.Indirect(rv).FieldByName(field.Name)
if value.Bool() {
fl++
}
}
if fl == 1 {
return nil
}
return fmt.Errorf("There are many options for methods. Please use one method for one call")
}