-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.ts
235 lines (213 loc) · 6.1 KB
/
Code.ts
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
const spreadName = 'google-visitor-manager'
const spreadId = findFile()
console.log('🚀🚀', spreadId)
function createSpreadSheet() {
var folderId = DriveApp.getRootFolder().getId()
var resource: GoogleAppsScript.Drive.Schema.File = {
title: spreadName,
//@ts-ignore
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{ id: folderId }],
}
var fileJson = Drive.Files.insert(resource)
var fileId = fileJson.id
modifySpread(
['FileID', 'Name', 'UserEmails', 'Expiration', 'isTrashed'],
fileId
)
console.log('🚀 Created Spread', fileId)
return fileId
}
function findFile() {
var root = DriveApp.getRootFolder()
var files = root.getFilesByName(spreadName)
if (files.hasNext()) {
return files.next().getId()
}
return createSpreadSheet()
}
function getVersions(myFileId: string, myEmails: string[]) {
var file = SpreadsheetApp.openById(spreadId)
var range = file.getDataRange()
var sheet = range.getValues()
var values = sheet.slice(1, sheet.length)
var versions = myEmails.map((_) => 0)
values.forEach((row) => {
let fileId = row[0]
let emails = (row[2] as string).split(',')
if (fileId == myFileId) {
myEmails.forEach((myEmail, i) => {
if (emails.indexOf(myEmail) > -1) versions[i] += 1
})
}
})
console.log('🚀', versions)
return versions
}
function getViewers(fileId: string) {
const file = DriveApp.getFileById(fileId)
const viewers = file.getViewers()
return viewers.map((viewer) => {
return viewer.getEmail()
})
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Picker')
.addItem('Start', 'showPicker')
.addToUi()
}
function showPicker() {
var html = HtmlService.createHtmlOutputFromFile('dialog.html')
.setWidth(600)
.setHeight(425)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
SpreadsheetApp.getUi().showModalDialog(html, 'Select a file')
}
function getOAuthToken() {
DriveApp.getRootFolder()
return ScriptApp.getOAuthToken()
}
function doGet() {
return HtmlService.createTemplateFromFile('form.html')
.evaluate()
.setTitle('Google GAS application')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
}
function modifySpread(contents: string[], id: string = spreadId) {
console.log('🚀 modify Spread', contents)
var file = SpreadsheetApp.openById(id)
file.appendRow(contents)
}
const customMessage = (
extraMessage: string,
expiration: string,
fileName: string,
version: number = 0
) => {
var verString = version == 0 ? '' : `_Ver${version}`
return `ProCube作業の作業報告書を送ります。
Filename: ${fileName}${verString}。
${extraMessage}。
このファイルは${expiration}には削除しますので、必要に応じてそれまでにダウンロードいただくようお願いします。`
}
function uploadFromDrive(e: any) {
// Read Form inputs
var arr = e['array[]'] as string[] | string
var ids = e.fileId as string
var expiration = e.expiration as string
var message = e.message as string
// var expirationOr = new Date(e.expiration)
// var expiration = new Date(expirationOr.getTime() - 9 * 60 * 60 * 1000)
// Get Files ids and emails
var emails = arr.toString().split(',')
var fileIds = ids.split(',')
if (fileIds.length == 1) {
var fileId = fileIds[0]
var fileName = DriveApp.getFileById(fileId).getName()
var versions = getVersions(fileId, emails)
shareFileToUsers(
emails,
fileId,
fileName,
expiration,
versions,
message
)
} else {
// Find folder where files are
var { id: zipId, name: zipName } = zipFiles(fileIds)
shareZipToUsers(emails, zipId, zipName, expiration, message)
}
}
function zipFiles(fileIds: string[]) {
var firstFile = DriveApp.getFileById(fileIds[0])
var folder = firstFile.getParents().next()
var names: Array<string> = []
var file = folder.createFile(
Utilities.zip(
fileIds.map((id, i) => {
var file_ = DriveApp.getFileById(id)
var name = file_.getName()
names.push(name)
return file_.getBlob().setName(name)
}),
names.join('<>') + '.zip'
)
)
console.log('🚀 Zip File Created', file.getId())
return { id: file.getId(), name: file.getName() }
}
function shareFileToUsers(
emails: string[],
fileId: string,
fileName: string,
expiration: string,
versions: number[],
extraMessage: string
) {
var date = new Date(expiration)
emails.forEach((email, i) => {
let body = customMessage(
extraMessage,
expiration,
fileName,
versions[i]
)
shareFileToUser(email, fileId, body, date)
})
modifySpread([
fileId,
fileName,
emails.toString(),
date.toISOString(),
'false',
])
}
function shareZipToUsers(
emails: string[],
fileId: string,
fileName: string,
expiration: string,
extraMessage: string
) {
var date = new Date(expiration)
emails.forEach((email) => {
let body = customMessage(extraMessage, expiration, fileName)
shareFileToUser(email, fileId, body, date)
})
modifySpread([
fileId,
fileName,
emails.toString(),
date.toISOString(),
'false',
])
}
function shareFileToUser(
email: string,
fileId: string,
body: string,
expiration: Date
) {
var permission = Drive.Permissions.insert(
{
value: email,
type: 'user',
role: 'reader',
withLink: false,
},
fileId,
{
sendNotificationEmails: true,
emailMessage: body,
}
)
Drive.Permissions.patch(
{
expirationDate: expiration.toISOString(),
},
fileId,
permission.id
)
}