【发布时间】:2020-09-18 15:28:57
【问题描述】:
我在 Google Sheet 中有一个日期对象,我想将它作为时间戳对象导入到 Firestore,但是当我直接执行时它不起作用。
data.date = new Date(dateSt);
如何将日期对象转换为时间戳。
完整代码:
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);
}
}
}
【问题讨论】:
-
为什么要标记 Excel?你在用 excel 工作吗?
-
我正在使用 Google 表格,抱歉出现错误
标签: database firebase google-apps-script google-cloud-firestore google-sheets-api