-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.js
189 lines (158 loc) · 5.84 KB
/
Utils.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
function setHeader(sheet, row, column, columnTitle, columnWidth=120) {
const headerStyle = SpreadsheetApp.newTextStyle()
.setBold(true)
.setFontFamily("Arial")
.setFontSize(11)
.build();
const richText = SpreadsheetApp.newRichTextValue()
.setText(columnTitle)
.setTextStyle(headerStyle)
.build();
sheet.getRange(row, column)
.setHorizontalAlignment(DocumentApp.HorizontalAlignment.CENTER)
.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP)
.setRichTextValue(richText)
.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP);
sheet.setColumnWidth(column, columnWidth);
}
function setColumn(sheet, row, column, columnWidth=120) {
sheet.getRange(row, column)
.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP)
.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP);
sheet.setColumnWidth(column, columnWidth);
}
function setWhenPossible(sheet, row, column, value) {
const isTextPermanent = sheet.getRange(row, column).getTextStyle().isItalic();
if (!isTextPermanent) {
setValue(sheet, row, column, value);
}
}
function setNote(sheet, row, column, value) {
sheet.getRange(row, column).setNote(value);
}
function unsetNote(sheet, row, column) {
sheet.getRange(row, column).clearNote();
}
function setValue(sheet, row, column, value) {
setValueByRange(sheet.getRange(row, column), value);
}
function setValueByRange(range, value) {
if (value) {
range.setFontColor(null)
.setHorizontalAlignment(DocumentApp.HorizontalAlignment.LEFT)
.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP)
.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP)
.setValue(value);
}
}
function setValuesByRow(sheet, row, values, startingColumn=1) {
setValuesByRange(sheet.getRange(row, startingColumn, values.length, values[0].length), values);
}
function setValuesByColumn(sheet, column, values, startingRow=2) {
setValuesByRange(sheet.getRange(startingRow, column, values.length, 1), values);
}
function setValuesByRange(range, values) {
if (values) {
range.setFontColor(null)
.setHorizontalAlignment(DocumentApp.HorizontalAlignment.LEFT)
.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP)
.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP)
.setValues(values);
}
}
function setRichTextValue(sheet, row, column, value) {
setRichTextValueByRange(sheet.getRange(row, column), value);
}
function setRichTextValueByRange(range, value) {
if (value) {
range.setFontColor(null)
.setHorizontalAlignment(DocumentApp.HorizontalAlignment.LEFT)
.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP)
.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP)
.setRichTextValue(value);
}
}
function setRichTextValuesByRow(sheet, row, values, startingColumn=1) {
setRichTextValuesByRange(sheet.getRange(row, startingColumn, 1, values[0].length), values);
}
function setRichTextValuesByColumn(sheet, column, values, startingRow=2) {
setRichTextValuesByRange(sheet.getRange(startingRow, column, values.length, 1), values);
}
function setRichTextValuesByRange(range, values) {
if (values) {
range.setFontColor(null)
.setHorizontalAlignment(DocumentApp.HorizontalAlignment.LEFT)
.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP)
.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP)
.setRichTextValues(values);
}
}
function setUrl(sheet, row, column, url, text="") {
setUrlByRange(sheet.getRange(row, column), url, text);
}
function setUrlByRange(range, url, text="") {
const richTextUrl = createRichTextUrl(url, (text == "") ? url : text);
setRichTextUrlByRange(range, richTextUrl);
}
function setRichTextUrl(sheet, row, column, richTextUrl) {
setRichTextUrlByRange(sheet.getRange(row, column), richTextUrl);
}
function setRichTextUrlByRange(range, richTextUrl) {
if (richTextUrl) {
range.setRichTextValue(richTextUrl);
}
}
function createRichTextUrl(url, text) {
return SpreadsheetApp.newRichTextValue()
.setText(text)
.setLinkUrl(url)
.build();
}
function setRangeColor(range, colorCode) {
range.setBackground(colorCode);
}
function setDataValidation(range, valueList, allowInvalid=false) {
const dataValidation = SpreadsheetApp.newDataValidation()
.requireValueInList(valueList)
.setAllowInvalid(allowInvalid)
.build();
range.setDataValidation(dataValidation);
}
function removeDataValidation(range) {
range.setDataValidation(null);
}
function setStrictProtection(range, description="Modification is not allowed") {
const protection = range.protect().setDescription(description);
const me = Session.getEffectiveUser();
protection.removeEditors(protection.getEditors().map(user => user.getEmail()))
protection.addEditor(me)
protection.setDomainEdit(false);
}
function alert(text) {
SpreadsheetApp.getUi().alert(text);
}
function prompt(text) {
return SpreadsheetApp.getUi().prompt(text).getResponseText();
}
function getTimeZone() {
return SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
}
function getValuesFromColumn(sheet, column) {
const range = sheet.getRange(2, column, sheet.getLastRow(), 1); // skip header
return range.getDisplayValues().flat();
}
function getFolderId() {
const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
const file = DriveApp.getFileById(spreadsheetId);
const parents = file.getParents();
if (parents.hasNext()) {
return parents.next().getId(); // Return the ID of the first parent folder
} else {
return DriveApp.getRootFolder().getId(); // Return the root folder ID if no parents exist
}
}
function getExportSheetUrl(sheet, format="xlsx") {
const spreadsheetUrl = sheet.getParent().getUrl().replace(new RegExp('/edit$'), "");
const sheetId = sheet.getSheetId();
return `${spreadsheetUrl}/export?gid=${sheetId}#gid=${sheetId}&format=${format}`;
}