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.
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);
}
}
}
Make sure your date value is a dates
and not a String
. Use DATEVALUE
to convert a String to a date the FirestoreApp can interpret as a timestamp.
Bonus: If you use =DATEVALUE('2020-07-11') + TIMEVALUE('18:00:00)
it will add the time to the timestamp in Firestore.
If you are using the library by "graham" 1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw The bug is fixed when using version 30 for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.