-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppScript.gs
88 lines (80 loc) · 3 KB
/
AppScript.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
88
function doGet(e) {
Logger.log( JSON.stringify(e) ); // view parameters
var result = 'Ok'; // assume success
if (e.parameter == 'undefined') {
result = 'No Parameters';
}
else {
var sheet_id = 'YOUR SPREADSHEET ID'; // Spreadsheet ID
var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet(); // get Active sheet
var newRow = sheet.getLastRow() + 1;
var rowData = [];
var d=new Date();
var DateTime_=Utilities.formatDate(d, "IST", "yyyy/MM/dd HH:mm");
rowData[0] = DateTime_; // Timestamp in column A
//rowData[0] = new Date(); // Timestamp in column A
for (var param in e.parameter) {
Logger.log('In for loop, param=' + param);
var value = stripQuotes(e.parameter[param]);
Logger.log(param + ':' + e.parameter[param]);
switch (param) {
case 'energy': //Parameter
rowData[1] = value; //Value in column B
result = 'Written on column B';
break;
default:
result = "unsupported parameter";
}
}
Logger.log(JSON.stringify(rowData));
// Write new row below
var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
newRange.setValues([rowData]);
}
// Return result of operation
firestore();
return ContentService.createTextOutput(result);
}
/**
* Remove leading and trailing single or double quotes
*/
function stripQuotes( value ) {
return value.replace(/^["']|['"]$/g, "");
}
function firestore(){
//add firebase library to script before running the code
//get firebase project credentials from GCP and add data from the created json key below
const email = "CLIENT EMAIL ID";
const key = "YOUR PRIVATE KEY";
const projectId = "YOUR PROJECT ID";
var firestore = FirestoreApp.getFirestore (email, key, projectId);
// get document data from ther spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "Elog";
var sheet = ss.getSheetByName(sheetname);
// get the last row and column in order to define range
var sheetLR = sheet.getLastRow(); // get the last row
var sheetLC = sheet.getLastColumn(); // get the last column
var dataSR = 2; // the first row of data
// define the data range
var sourceRange = sheet.getRange(2,1,sheetLR-dataSR+1,sheetLC);
// get the data
var sourceData = sourceRange.getValues();
// get the number of length of the object in order to establish a loop value
var sourceLen = sourceData.length;
// Loop through the rows
for (var i= sourceLen-1;i<sourceLen;i++){
if (sourceData[i][1] !=='') {
var data = {};
var dateSt = sourceData[i][0].toString();
var stDate = new Date(dateSt);
var d=Utilities.formatDate(stDate, "IST", "yyyy/MM/dd HH:mm");
var stringfied = JSON.stringify(d);
var updatedDt = stringfied.slice(1,11)+' '+ stringfied.slice(12,18);
data.dateTime = updatedDt;
data.energy = sourceData[i][1];
data.units=sourceData[i][2];
firestore.createDocument("database",data);
}
}
}