我正在尝试编写此功能,理想情况下会返回Google电子表格中单元格的范围。我认为我提出的代码是可以的并且通常会有效,但不幸的是,由于此代码,脚本超出了时间限制。
因此,我想知道是否有更好(并且最重要的是更快)的方式来获得相关细胞的位置。
以下是我的代码:
function appendNewToDashboardSS (folderId){
var folderName = DriveApp.getFolderById('***').getName();
var latestFileId = getLatestFileId('***');//get the id of the last uploaded file in the folder
Logger.log(latestFileId);
var formatedName = DriveApp.getFileById(latestFileId).getName().replace(".csv", "");//format the name so it doesn't include the .csv part
DriveApp.getFileById(latestFileId).setName(formatedName);//set the name
var ss = SpreadsheetApp.openById(latestFileId);//open the file
var rangeStart = 0;
var rowCount = ss.getLastRow();
for (i=1; i <= rowCount; i++) {
var cellValue = ss.getRange("A" + i).getValue();
if(cellValue===formatedName){
rangeStart = i; //this is where the needed range would start
break;
}
}
Logger.log("range start" + rangeStart);
}
答案 0 :(得分:2)
您应该读取整个工作表数据并循环到结果数组中,而不是逐个获取每个单元格数据。速度差异真的很明显。 这应该做的工作:
function appendNewToDashboardSS (folderId){
var folderName = DriveApp.getFolderById('***').getName();
var latestFileId = getLatestFileId('***');//get the id of the last uploaded file in the folder
Logger.log('latestFileId='+latestFileId);
var formatedName = DriveApp.getFileById(latestFileId).getName().split(".csv")[0];//keep only the part before .csv part
DriveApp.getFileById(latestFileId).setName(formatedName);//set the name
var ss = SpreadsheetApp.openById(latestFileId);//open the file
var rangeStart;
var data = ss.getActiveSheet().getDataRange().getValues();
for (i=0; i <= data.lengt-1; i++) {
var cellValue = data[i][0];
if(cellValue.split('.csv')[0]==formatedName){
rangeStart = i+1; //this is where the needed range would start
break;
}
}
Logger.log("range start" + rangeStart);
}