-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.js
62 lines (53 loc) · 1.66 KB
/
json.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
var COLUMN_MAX = 26;
var spreadsheetToJson = function (options) {
var {
sheetName,
rowBetween = [0, 0],
columnMax = COLUMN_MAX
} = options;
rowBetween[0] = parseInt(rowBetween[0], 10);
rowBetween[1] = parseInt(rowBetween[1], 10);
// Our working sheet
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
// Header for our json
var header = sheet.getRange(1, 1, 1, columnMax).getValues()[0];
// Data to output in json
var data = sheet.getRange(rowBetween[0], 1, rowBetween[1] - rowBetween[0], columnMax).getValues();
// our json result
var jsonArr = [];
// rows loop
for (var i = 0, iLen = data.length; i < iLen; i++) {
var json = {};
// columns loop
for (var j = 0, jLen = header.length; j < jLen; j++) {
json[header[j]] = data[i][j];
}
// Create and save reference of the dataline number this json.
json.line_number = rowBetween[0] + i;
jsonArr.push(json);
}
return jsonArr;
}
var jsonToSpreadsheet = function (options) {
var {
sheetName,
data,
columnMax = COLUMN_MAX
} = options;
// Our working sheet
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
// Get range for updated expenses; getRange(LINE, COLUMN)
var range = sheet.getRange(1, 1, data.length, columnMax);
// update range
range.setValues(data);
}
var updatePosition = function (options) {
var {
sheetName,
data,
x,
y
} = options;
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
return sheet.getRange(x, y).setValue(data);
}