forked from JPratama7/gwrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
124 lines (109 loc) · 2.95 KB
/
docs.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
package docs
import (
"context"
"errors"
"log"
"github.com/JPratama7/safe"
"github.com/aiteung/gwrap"
"google.golang.org/api/docs/v1"
"google.golang.org/api/option"
)
func DocsNewServices(FileCred string, FileToken string) (docsService GoogleDocs) {
cfgGoogle, err := gwrap.NewGoogleConfig(FileCred, docs.DocumentsScope, docs.DocumentsReadonlyScope)
if err != nil {
return
}
client := gwrap.GetClient(cfgGoogle, FileToken)
curCtx := context.Background()
srvDocs := safe.AsResult(docs.NewService(curCtx, option.WithHTTPClient(client)))
if srvDocs.IsErr() {
log.Print("Google Drive or Docs Service Unavailable")
}
docsService = NewGoogleDocs(srvDocs.Unwrap())
return
}
type GoogleDocs struct {
srv *docs.Service
}
func NewGoogleDocs(srvDocs *docs.Service) (res GoogleDocs) {
res = GoogleDocs{srv: srvDocs}
return
}
func (gdocs *GoogleDocs) Service() *docs.Service {
return gdocs.srv
}
func (gdocs *GoogleDocs) FindAndReplace(docId string, request ...*docs.Request) (err error) {
_, err = gdocs.srv.Documents.BatchUpdate(docId, &docs.BatchUpdateDocumentRequest{Requests: request}).Do()
if err != nil {
return
}
return
}
func (gdocs *GoogleDocs) FindTextLocation(docId string, pattern string) (loc docs.Location, err error) {
file, err := gdocs.srv.Documents.Get(docId).Do()
if len(file.Body.Content) < 1 {
return
}
for _, element := range file.Body.Content {
switch {
case element.Paragraph != nil:
if data := searchTextElement(pattern, element.Paragraph.Elements...); data != nil {
loc.Index = (data.StartIndex + data.EndIndex) / 2
return
}
case element.Table != nil:
if len(element.Table.TableRows) < 1 {
return
}
for _, v := range element.Table.TableRows {
if len(v.TableCells) < 1 {
continue
}
for _, v2 := range v.TableCells {
if len(v2.Content) < 1 {
continue
}
for _, v3 := range v2.Content {
if v3.Paragraph == nil {
continue
}
if data := searchTextElement(pattern, v3.Paragraph.Elements...); data != nil {
loc.Index = data.StartIndex
return
}
}
}
}
case element.TableOfContents != nil:
if len(element.TableOfContents.Content) < 1 {
return
}
for _, v := range element.TableOfContents.Content {
if len(v.Paragraph.Elements) < 1 {
continue
}
if data := searchTextElement(pattern, v.Paragraph.Elements...); data != nil {
loc.Index = data.StartIndex
}
}
}
}
return
}
func (gdocs *GoogleDocs) GetTableLocation(docID string) (data *docs.Location, err error) {
doc, err := gdocs.srv.Documents.Get(docID).Do()
if err != nil {
log.Fatalf("Unable to retrieve document: %v", err)
}
for _, content := range doc.Body.Content {
if content.Table != nil {
return &docs.Location{
Index: (content.StartIndex + content.EndIndex) / 2,
ForceSendFields: content.ForceSendFields,
NullFields: content.NullFields,
}, nil
}
}
err = errors.New("table not found")
return
}