-
Notifications
You must be signed in to change notification settings - Fork 1
/
obscure_addresses_gsheets.gs
66 lines (53 loc) · 1.53 KB
/
obscure_addresses_gsheets.gs
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
var ui = SpreadsheetApp.getUi();
var addressColumn = 1;
var outputColumn = 2;
function processAddress(){
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getActiveRange();
if (cells.getNumColumns() != 2) {
ui.alert(
'Warning',
'You must select 2 columns: Address, Obscured Address',
ui.ButtonSet.OK
);
return;
}
for (addressRow = 1; addressRow <= cells.getNumRows(); addressRow++) {
var address = cells.getCell(addressRow, addressColumn).getValue();
if (!address) {continue}
obscureAddress(cells, addressRow, address);
}
}
function obscureAddress(cells, row, address){
// pluck first set of digits at the beginning of the address string
var myregexp = /^\d+/g;
var match = myregexp.exec(address);
if (!match) {
insertDataIntoSheet(cells, row, [
[outputColumn, address]
]);
}
else {
var street_num = match[0];
// replaces last 2 digits with zeroes
var street_num_obscured = street_num.slice(0, -2) + "00";
// return obscured address
var obscured_address = address.replace(street_num, street_num_obscured);
insertDataIntoSheet(cells, row, [
[outputColumn, obscured_address]
]);
}
}
/**
* Sets cells from a 'row' to values in data
*/
function insertDataIntoSheet(cells, row, data) {
for (d in data) {
cells.getCell(row, data[d][0]).setValue(data[d][1]);
}
}
function onOpen() {
ui.createMenu('Obscure Address')
.addItem('Obscure selected addresses', 'processAddress')
.addToUi();
}