-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
131 lines (115 loc) · 4.62 KB
/
Copy pathCode.gs
File metadata and controls
131 lines (115 loc) · 4.62 KB
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
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Scripts').addItem('Message Generator', 'showMessages').addItem('Template Creator', 'showMessagesConfig').addItem('Template Manager', 'showMessagesManager').addToUi();
}
// Show the template manager popup
function showMessagesManager() {
var html = HtmlService.createHtmlOutputFromFile('messagemanager').setWidth(600).setHeight(450);
SpreadsheetApp.getUi().showModalDialog(html, 'Template Manager');
}
// Delete a template
function deleteTemplate(template) {
var curr = getTemplates();
var index = curr.indexOf(template);
if (index != -1) {
curr.splice(index, 1);
}
PropertiesService.getDocumentProperties().setProperty("TEMPLATELIST", JSON.stringify(curr));
PropertiesService.getDocumentProperties().deleteProperty("TEMPLATE_" + template);
PropertiesService.getDocumentProperties().deleteProperty("MAPPINGS_" + template);
}
// Show the message generator popup
function showMessages() {
var html = HtmlService.createHtmlOutputFromFile('message').setWidth(600).setHeight(450);
SpreadsheetApp.getUi().showModalDialog(html, 'Message Generator');
}
// Show the config menu popup
function showMessagesConfig() {
var html = HtmlService.createHtmlOutputFromFile('messageconf').setWidth(600).setHeight(450);
SpreadsheetApp.getUi().showModalDialog(html, 'Template Creator');
}
// Get all column headers
function getColumns() {
return SpreadsheetApp.getActiveSheet().getRange(1, 1, 1, SpreadsheetApp.getActiveSheet().getLastColumn()).getValues()[0];
}
// Get a list of all templates
function getTemplates() {
var templates = JSON.parse(PropertiesService.getDocumentProperties().getProperty("TEMPLATELIST"));
if (!templates) {
return [];
}
return templates;
}
// Get a template
function getTemplate(name) {
return JSON.parse(PropertiesService.getDocumentProperties().getProperty("TEMPLATE_" + name));
}
// Create a new template
function newTemplate(name, content, mappings) {
var templates = JSON.parse(PropertiesService.getDocumentProperties().getProperty('TEMPLATELIST'));
if (templates) {
templates.push(name);
PropertiesService.getDocumentProperties().setProperty('TEMPLATELIST', JSON.stringify(templates));
} else {
PropertiesService.getDocumentProperties().setProperty('TEMPLATELIST', JSON.stringify([name]));
}
PropertiesService.getDocumentProperties().setProperty("TEMPLATE_" + name, JSON.stringify(content));
mappings.forEach(mapping => {
addMapping(name, mapping[1], mapping[0]);
})
}
// Get template values
function getTemplateValues(name, row) {
var parsed = [];
var mappings = getMappings(name);
var sheet = SpreadsheetApp.getActiveSheet();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0].map(h => (h === null || h === undefined ? "" : String(h).trim()));
var values = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getDisplayValues()[0];
mappings.forEach(mapping => {
var [key, source] = mapping;
source = String(source).trim();
var pos = headers.indexOf(source);
if (pos == -1) {
return;
}
parsed.push([key, values[pos]]);
});
return JSON.stringify(parsed);
}
// Get all the mappings for a certain template
function getMappings(name) {
return JSON.parse(PropertiesService.getDocumentProperties().getProperty("MAPPINGS_" + name));
}
// Saves a mapping to the documents local storage
function addMapping(name, key, source) {
var mappings = JSON.parse(PropertiesService.getDocumentProperties().getProperty('MAPPINGS_' + name));
if (mappings) {
mappings.push([key, source])
PropertiesService.getDocumentProperties().setProperty('MAPPINGS_' + name, JSON.stringify(mappings));
} else {
PropertiesService.getDocumentProperties().setProperty('MAPPINGS_' + name, JSON.stringify([[key, source]]));
}
}
// Get the rows in the active sheet
function getRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getRange("A1:A" + sheet.getLastRow()).getValues().flat();
return values;
}
// Alias for getColumnIdByValue(...) that targets the active sheet specifically
function getRowNumber(name) {
return getColumnIdByValue(name, SpreadsheetApp.getActiveSheet());
}
// A simple function to get the column containg a certain value on a certain sheet - this is copied from one of my other gscript projects
function getColumnIdByValue(searchValue, sheet) {
var lastRow = sheet.getLastRow();
var dataRange = sheet.getRange("A1:A" + lastRow);
var data = dataRange.getValues();
for (var row = 0; row < lastRow; row++) {
if (data[row].toString() == searchValue) {
var rowId = row;
return (rowId + 1);
}
}
return null;
}