-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocs.js
203 lines (176 loc) · 6.69 KB
/
Docs.js
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
const PROPERTY_SPREADSHEET_ID = 'foiamail.spreadsheet_id'
function extractHeaders(ss) {
const sheet = ss.getSheets()[0];
// This represents ALL the data
const range = sheet.getDataRange();
const values = range.getValues();
return values[0]
}
/**
* TODO: make this insert a placeholder token in the document
* @param {*} params a JSON object including our chosen parameters and other metadata
*/
function insertHeaderPlaceholder(params) {
const html_output = HtmlService
.createHtmlOutput(
// JSON.stringify(params)
params.parameters.header,
)
.setWidth(250)
.setHeight(300);
DocumentApp.getUi().showModalDialog(html_output,'woopitydoopdop')
}
function onDocsHomepage(e){
const props = PropertiesService.getScriptProperties()
const spreadsheet_id = props.getProperty(PROPERTY_SPREADSHEET_ID)
let ss;
if (spreadsheet_id) {
ss = SpreadsheetApp.openById(spreadsheet_id)
}
let docsEventObject = e['docs'];
let builder = CardService.newCardBuilder();
// Assemble the widgets and return the card.
var card = CardService.newCardBuilder()
.setFixedFooter(createFixedFooter());
if (ss) {
const has_spreadsheet_section = CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText(
`Spreadsheet selected: [${ss.getName()}]`
))
.addWidget(
CardService.newTextButton()
.setText('Open Spreadsheet')
.setOpenLink(
CardService.newOpenLink()
.setUrl(ss.getUrl())
)
)
card.addSection(has_spreadsheet_section)
const headers = extractHeaders(ss)
console.log(`headers: ${headers}`)
const insert_placeholders_section = CardService.newCardSection()
insert_placeholders_section.setHeader("Insert placeholders")
// insert_placeholders_section.addWidget(
// CardService.newTextParagraph().setText(`headers: ${headers}`)
// )
for (let i = 0; i < headers.length; i++) {
const h = headers[i];
insert_placeholders_section.addWidget(
CardService.newTextButton()
.setText(h)
.setOnClickAction(
CardService.newAction()
.setFunctionName('insertHeaderPlaceholder')
.setParameters(
{'header': h}
)
))
}
// headers.forEach(element => {
// if (!element.trim().match(/^$/)) { // don't mess with blanks
// insert_placeholders_section.addWidget(
// CardService.newTextButton()
// .setText(element)
// )
// }
// });
card.addSection(insert_placeholders_section)
}
const link_sheet_button = CardService.newTextButton()
.setText('Select Google Sheets document for FOIAMail Merge')
.setOnClickAction(
CardService.newAction()
.setFunctionName('showPicker'));
card.addSection(
CardService.newCardSection()
.addWidget(link_sheet_button)
)
return card.build()
}
function pickerSelectionHandler(e) {
const props = PropertiesService.getScriptProperties()
props.setProperty(PROPERTY_SPREADSHEET_ID,e.document.id)
var builder = CardService.newCardBuilder();
// var cardSection = CardService.newCardSection();
// cardSection.setHeader('Picker Picked')
// cardSection.addWidget(
// CardService.newTextParagraph().setText(
// )
// )
// builder.addSection(cardSection)
// console.log('pickerSelectionHandler')
// built = builder.build()
// console.log(built)
DocumentApp.getUi().showSidebar
(HtmlService
.createHtmlOutput(`<b>${JSON.stringify(e)}}</b>`)
.setTitle(STANDARD_SIDEBAR_TITLE)
)
}
/**
* For the current document, display either its quota information or
* a button that allows the user to provide permission to access that
* file to retrieve its quota details.
*
* @param e The event containing information about the current document
* @return {Card}
*/
function createAddOnViewExample(e) {
var docsEventObject = e['docs'];
var builder = CardService.newCardBuilder();
var cardSection = CardService.newCardSection();
if (docsEventObject['addonHasFileScopePermission']) {
cardSection.setHeader(docsEventObject['title']);
// This add-on uses the recommended, limited-permission `drive.file`
// scope to get granular per-file access permissions.
// See: https://developers.google.com/drive/api/v2/about-auth
// If the add-on has access permission, read and display its quota.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"here's some text"));
} else {
// If the add-on does not have access permission, add a button that
// allows the user to provide that permission on a per-file basis.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"The add-on needs permission to access this file's quota."));
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClicked");
var button = CardService.newTextButton()
.setText("Request permission")
.setOnClickAction(buttonAction);
cardSection.addWidget(button);
}
return builder.addSection(cardSection).build();
}
/**
* Read the Google Doc identified by the given ID.
* Returns an object with two properties:
* `subject` and `body`
* @param {String} doc_id
* @returns {Object} parsed
*/
function parseDocToTemplate(doc_id) {
let doc = DocumentApp.openById(doc_id);
let body = doc.getBody()
let text = body.getText()
return {
body: text,
subject: `We'll parse the subject later ${new Date().toDateString()}`
}
}
/**
* Displays an HTML-service dialog in Google Sheets that contains client-side
* JavaScript code for the Google Picker API.
*/
function showPicker() {
var html = HtmlService.createHtmlOutputFromFile('Picker.html')
.setWidth(600)
.setHeight(425)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
DocumentApp.getUi().showModalDialog(html, 'Select Spreadsheet');
}
function getOAuthToken() {
DriveApp.getRootFolder();
return ScriptApp.getOAuthToken();
}