我已使用以下查询将数据从 Googlesheet1 复制到 Googlesheet2
=IMPORTRANGE("url","!A2:H")
这已将数据从 Googlesheet1 复制到 Googlesheet2。
在那张表中,我有一个如下图所示的持续时间列
当我使用应用程序脚本将数据复制到 firestore 而不是保存持续时间时,它会以 DateTime 格式保存数据,如下所示。
有没有办法将给定的持续时间转换为 Google 表格中的秒数。
我尝试使用 =value(G2*24*3600)
但它在 Googlesheet2 中不起作用,因为该工作表是 Googlesheet1 的克隆
应用程序脚本逻辑:
function firestore() {
// Firestore setup
const email = "//client-email";
const key = "//client-key";
const projectId = "timesheet-aog";
var firestore = FirestoreApp.getFirestore (email, key, projectId);
// get document data from ther spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "timesheet";
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;
console.log('sourceLen is', sourceLen);
// Loop through the rows
for (var i=0;i<sourceLen;i++){
var data = {};
console.log('data is', sourceData);
data.date = sourceData[i][0];
data.name = sourceData[i][1];
data.workFrom = sourceData[i][2];
data.project = sourceData[i][3];
data.phase = sourceData[i][4];
data.task = sourceData[i][5];
data.totalHrs = sourceData[i][6];
data.comments = sourceData[i][7];
firestore.createDocument("timesheet",data);
}
}
最佳答案
以下是第二个工作表的 A1
单元格的公式:
={
IMPORTRANGE("url","!A2:F"),
ARRAYFORMULA(
IF(
IMPORTRANGE("url","!G2:G") = "",
"",
N(IMPORTRANGE("url","!G2:G")) * 24 * 3600
)
),
IMPORTRANGE("url","!H2:H")
}
尝试在原始工作表中使用列的命名范围(A2:F
、G2:G
、H2:H
),然后导入它们按这些名称,因此您无需调整使用确切列名称的公式。
关于firebase - 如何在使用 IMPORTRANGE 的 Google 表格中将持续时间转换为秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60866831/