-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.ts
162 lines (142 loc) · 5.27 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
function getSheetData(sheetName?: string) {
const sheet = sheetName
? SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
: SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = sheet.getDataRange().getValues();
return data;
}
function getSheetNames() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
return sheets.map(sheet => sheet.getName());
}
function writeColumnCombos(opts: {src?: string, dest: string, header?: boolean}) {
const sheetData = opts.src ? getSheetData(opts.src) : getSheetData();
const destSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(opts.dest);
if (opts.header) {
const headerRow = sheetData.slice(0, 1);
const combos = combinations(rowsToColumnsFlat(sheetData.slice(1)));
writeData(headerRow.concat(combos), destSheet);
} else {
const columns = rowsToColumnsFlat(sheetData);
const columnGroups = arraySplit(columns, x => x.length === 0);
const flattened = ensureArray2d(columnGroups, rotateAndJoin);
writeData(combinations(flattened), destSheet);
}
}
function rowsToColumns(arr2d: any[][]) {
const longest = arr2d.reduce((maxI, el, i, arr) => (el.length > arr[maxI].length) ? i : maxI, 0);
return arr2d[longest].map((x, i) => arr2d.map(x => x[i]));
}
function rowsToColumnsFlat(arr2d: any[][]) {
const maxLen = arr2d.reduce((a, c) => Math.max(a, c.length), 0);
const transposed = Array.from(Array(maxLen), () => []);
arr2d.forEach((row, i) => {
for (let j = 0; j < maxLen; j++) {
if (row[j]) transposed[j].push(row[j]);
}
});
return transposed;
}
function writeData(data: any[][], destination: any, startRow: number = 1, startCol: number = 1) {
const numColumns = data[0].length; // assumes data is uniform
const numRows = data.length;
const dest = destination || SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
dest.getRange(startCol, startRow, numRows, numColumns).setValues(data);
}
function writeSheetData() {
const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const dataRange = activeSheet.getDataRange();
activeSheet.getRange(
dataRange.getNumRows() + 1,
dataRange.getColumn(),
dataRange.getNumRows() - 1,
dataRange.getNumColumns()
).setValues(dataRange.getValues().slice(1));
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Scripts')
.addItem('Write Sheet Data', 'writeSheetData')
.addItem('Set preferred name', 'showPrompt')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
const preferredName: string = getUserName();
if (preferredName) {
const ui = SpreadsheetApp.getUi();
ui.alert(`Welcome back ${preferredName}!`);
}
}
function showPrompt() {
const ui = SpreadsheetApp.getUi();
const result = ui.prompt(
'Let\'s get to know each other!',
'Please enter your preferred name:',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
const button = result.getSelectedButton();
const name = result.getResponseText();
if (button == ui.Button.OK) {
setUserName(name);
ui.alert(`Your preferred name is ${name}.`); // User clicked "OK".
} else if (button == ui.Button.CANCEL) {
ui.alert('I didn\'t get your name.'); // User clicked "Cancel".
} else if (button == ui.Button.CLOSE) {
ui.alert('You closed the dialog.'); // User clicked X in the title bar.
}
}
function showSidebar() {
const html = HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('My custom sidebar');
SpreadsheetApp.getUi().showSidebar(html); // Or DocumentApp or SlidesApp or FormApp.
}
function combinations(arr2d:any[][]) {
const r = [];
const af = arr2d.filter(x => x.length > 0); // no blank arrays allowed
const max = af.length - 1;
function helper(arr:any[], i:number) {
for (let j = 0, l = af[i].length; j < l; j++) {
const a = arr.slice(0);
a.push(af[i][j]);
if (i === max) r.push(a);
else helper(a, i + 1);
}
}
helper([], 0);
return r;
}
function setUserName(name: string) {
PropertiesService.getUserProperties().setProperty('PREFERRED_NAME', name);
}
function getUserName() {
return PropertiesService.getUserProperties().getProperty('PREFERRED_NAME');
}
function arraySplit(arr: any[], condition: Function) {
const result = [];
let i = 0;
arr.forEach(x => condition(x) ? i++ : result[i] ? result[i].push(x) : result[i] = [x]);
return result;
}
function ensureArray2d(arr: any[][], action: Function) {
const arr2d = arr.slice();
arr2d.forEach((x, i) => (x.length > 1)
? arr2d[i] = action(x)
: arr2d[i] = x[0]);
return arr2d;
}
function rotateAndJoin(arr: any[], str: string = ', ') {
return rowsToColumnsFlat(arr).map(x => x.join(str));
}
// LOCAL TESTING ONLY
const testData = [
["TASK", "", "FIRST NAME", "NICKNAME", "TITLE", "", "DAY"],
["automation", "", "Matthew", "Matt", "Tech Dir", "", "Mon"],
["front-end", "", "Jorge", "Chorch", "Tech Lead", "", "Tue"],
["", "", "Agustín", "Agus", "Sr Creative Tech", "", "Wed"],
["", "", "", "", "", "", "Thu"],
["", "", "", "", "", "", "Fri"]
];
const columns = rowsToColumnsFlat(testData.slice(1));
console.log(columns);
const columnGroups = arraySplit(columns, x => x.length === 0);
console.log(columnGroups);
const flattened = ensureArray2d(columnGroups, rotateAndJoin);
console.log(combinations(flattened));