How to convert Date (from google sheet data) to TimeStamp (Firestore) using AppScript?

亡梦爱人 提交于 2020-06-16 17:24:28

问题


I've got a date object in Google Sheet which I want to import to firestore as a timestamp object, but it doesn't work when I do it directly.

If I enter the current date,

 data.date = new Date(dateSt);

it stores an empty map object in firestore.

What should I do to convert the date object to timestamp.

Full Code:

function classRoutineFunction() {
   const email = "my_email";
   const key = "my_private_key";
   const projectId = "my_projectID";
   var firestore = FirestoreApp.getFirestore (email, key, projectId);


  // get document data from ther spreadsheet
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheetname = "Sheet1";
   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=0;i<sourceLen;i++){
     if(sourceData[i][1] !== '') {
       var data = {};
       var dateSt = sourceData[i][0].toString();
       var stDate = new Date(dateSt);
       var stringfied = JSON.stringify(stDate);
       var updatedDt = stringfied.slice(1,11);

       data.date = new Date(dateSt);
       data.time = sourceData[i][1];
       data.batch = sourceData[i][2];
       data.topic = sourceData[i][3];

       firestore.createDocument("classRoutine",data);
     }

  }
}

来源:https://stackoverflow.com/questions/62113437/how-to-convert-date-from-google-sheet-data-to-timestamp-firestore-using-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!