-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumberSplitter.ts
More file actions
53 lines (45 loc) · 2.07 KB
/
numberSplitter.ts
File metadata and controls
53 lines (45 loc) · 2.07 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
function onOpen() {
const spreadsheet = SpreadsheetApp.getActive()
let menuItems = [
{ name: 'Split Phone Numbers...', functionName: 'splitPhones' }
]
spreadsheet.addMenu('PhoneNumber Hacks', menuItems)
}
const splitPhones = () => {
let ui = SpreadsheetApp.getUi()
let phoneResponse = ui.prompt("Enter Comma Seperated Values for the phone columns (Or leave blank to just map the data columns)")
let phoneColumns = []
if (phoneResponse.getSelectedButton() !== ui.Button.CLOSE) {
phoneColumns = phoneResponse.getResponseText().split(',').map(chars => chars.trim())
}
let infoColumns: string[] = [] // ['F', 'T', 'R']
let infoResponse = ui.prompt("Enter Comma Seperated Values for the info you want")
if (infoResponse.getSelectedButton() !== ui.Button.CLOSE) {
infoColumns = infoResponse.getResponseText().split(',').map(chars => chars.trim())
}
let originalSheet = SpreadsheetApp.getActiveSheet()
let splitSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet('Split Numbers')
// Assume we have headers on our original
const infoHeaders: string[] = infoColumns
// ['F', 'T', 'R'] => [Range(F1), Range(T1), Range(R1)] => ["first name", "last name", "address"...]
.map(letter => originalSheet.getRange(`${letter}1`))
.map(range => range.getValue())
infoHeaders.push("Phone")
splitSheet.appendRow(infoHeaders)
// for row in our sheet
// get the row,
// cycle through the phone columns
// if that phone exists, copy our data columns + the phone colum to the new sheet
const lastRow = originalSheet.getLastRow()
for (let row = 2; row <= lastRow; row++) {
let infoData = infoColumns
.map(letter => originalSheet.getRange(`${letter}${row}`))
.map(range => range.getValue())
for (let phoneCol of phoneColumns) {
let currNum = originalSheet.getRange(`${phoneCol}${row}`)
if (!currNum.isBlank()) {
splitSheet.appendRow(infoData.concat(currNum.getValue()))
}
}
}
}