谷歌电子表格。我动态删除旧工作表并创建(我使用sheet.copyTo()函数)具有相同名称的新工作表,但 IMPORTRANGE 公式无法(定期)找到新工作表。
=ImportRange("ssId", "Data!A1:B1")
此脚本工作正常,但其他通过 IMPORTRANGE 公式定期从此电子表格中获取数据的电子表格无法找到新的(生成的)表格:
=QUERY(IFERROR(IMPORTRANGE("ssId", "Data!A:AU")),"SELECT * ", 0)
这个公式不返回任何东西!
function copySpreadSheet(sourceId, targetId) {
try {
var sourceSS = SpreadsheetApp.openById(sourceId);
SpreadsheetApp.setActiveSpreadsheet(sourceSS);
var sourceSheets = sourceSS.getSheets();
var targetSS = SpreadsheetApp.openById(targetId);
SpreadsheetApp.setActiveSpreadsheet(targetSS);
var targetSheets = targetSS.getSheets();
//you can't delete all sheets!
var timestamp = new Date().getTime();
var tempName = "TEMPORARY-FOR-DELETE-" + timestamp;
targetSS.insertSheet(tempName);
for(var i = 0; i < targetSheets.length; i++) {
targetSS.deleteSheet(targetSheets[i]);
}
var sourceCharts = [];
var newSheetCharts = [];
var newSheet = null;
var newSheetName = "";
for(var i = 0; i < sourceSheets.length; i++) {
newSheet = sourceSheets[i].copyTo(targetSS)
newSheetName = newSheet.getName().replace("Copy of ", "");
newSheet.setName(newSheetName);
/*
newSheetCharts = newSheet.getCharts();
sourceCharts = sourceSheets[i].getCharts();
for(var j = 0; j < sourceCharts.length; j++) {
/* Server Error:
var chart = newSheetCharts[j].modify()
.setChartType(sourceCharts[j].getType())
.addRange(sourceCharts[j].getRange())
.build();
newSheet.updateChart(chart);
*/
//insertImage(blob, column, row, offsetX, offsetY)
/*
newSheet.insertImage(
sourceCharts[j].getBlob(),
sourceCharts[j].getContainerInfo().getAnchorColumn(),
sourceCharts[j].getContainerInfo().getAnchorRow(),
sourceCharts[j].getContainerInfo().getOffsetX(),
sourceCharts[j].getContainerInfo().getOffsetY()
);
*/
/* } */
}
targetSS.deleteSheet(targetSS.getSheetByName(tempName)); //remove a temporary sheet
//Adding Date Stamp:
targetSS.insertSheet("Last time updated").hideSheet().getRange(1, 1).setValue(new Date());
SpreadsheetApp.flush();
Utilities.sleep(500);
return targetSS;
} catch (err) {
Logger.log(err.toString());
}
}
我试图“欺骗”电子表格:
var querySheet = dataSourceSS.getSheetByName("Query");
querySheet.activate();
Logger.log(formula);
querySheet.getRange(2, 1).setFormula("=Minute(Now())"); //any FORMULA
Utilities.sleep(1000);
querySheet.getRange(2, 1).setFormula(formula);