-
Notifications
You must be signed in to change notification settings - Fork 32
/
GoogleScripts-example.gs
88 lines (62 loc) · 3.67 KB
/
GoogleScripts-example.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Example Google Scrips code to upload data to Google Sheets from Arduino/ESP8266
// Follow setup instructions found here:
// https://github.com/StorageB/Google-Sheets-Logging
//
// email: [email protected]
// Enter Spreadsheet ID here
var SS = SpreadsheetApp.openById('3213k-okfjdk-gkeJHjd87-vdKei-lKQDIc-a2a_Hjg4');
var str = "";
function doPost(e) {
var parsedData;
var result = {};
try {
parsedData = JSON.parse(e.postData.contents);
}
catch(f){
return ContentService.createTextOutput("Error in parsing request body: " + f.message);
}
if (parsedData !== undefined){
var flag = parsedData.format;
if (flag === undefined){
flag = 0;
}
var sheet = SS.getSheetByName(parsedData.sheet_name); // sheet name to publish data to is specified in Arduino code
var dataArr = parsedData.values.split(","); // creates an array of the values to publish
// Default time zone is America/Chicago. Update time zone below with appropriate ID from here: https://developers.google.com/google-ads/api/data/codes-formats#timezone-ids
var date_now = Utilities.formatDate(new Date(), "America/Chicago", "yyyy/MM/dd"); // gets the current date
var time_now = Utilities.formatDate(new Date(), "America/Chicago", "hh:mm:ss a"); // gets the current time
var value0 = dataArr [0]; // value0 from Arduino code
var value1 = dataArr [1]; // value1 from Arduino code
var value2 = dataArr [2]; // value2 from Arduino code
// read and execute command from the "payload_base" string specified in Arduino code
switch (parsedData.command) {
case "insert_row":
sheet.insertRows(2); // insert full row directly below header text
//var range = sheet.getRange("A2:D2"); // use this to insert cells just above the existing data instead of inserting an entire row
//range.insertCells(SpreadsheetApp.Dimension.ROWS); // use this to insert cells just above the existing data instead of inserting an entire row
sheet.getRange('A2').setValue(date_now); // publish current date to cell A2
sheet.getRange('B2').setValue(time_now); // publish current time to cell B2
sheet.getRange('C2').setValue(value0); // publish value0 from Arduino code to cell C2
sheet.getRange('D2').setValue(value1); // publish value1 from Arduino code to cell D2
sheet.getRange('E2').setValue(value2); // publish value2 from Arduino code to cell E2
str = "Success"; // string to return back to Arduino serial console
SpreadsheetApp.flush();
break;
case "append_row":
var publish_array = new Array(); // create a new array
publish_array [0] = date_now; // add current date to position 0 in publish_array
publish_array [1] = time_now; // add current time to position 1 in publish_array
publish_array [2] = value0; // add value0 from Arduino code to position 2 in publish_array
publish_array [3] = value1; // add value1 from Arduino code to position 3 in publish_array
publish_array [4] = value2; // add value2 from Arduino code to position 4 in publish_array
sheet.appendRow(publish_array); // publish data in publish_array after the last row of data in the sheet
str = "Success"; // string to return back to Arduino serial console
SpreadsheetApp.flush();
break;
}
return ContentService.createTextOutput(str);
} // endif (parsedData !== undefined)
else {
return ContentService.createTextOutput("Error! Request body empty or in incorrect format.");
}
}