问题
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