gpt4 book ai didi

javascript - 加速 UrlFetch Google 应用程序脚本?

转载 作者:行者123 更新时间:2023-12-01 03:13:44 27 4
gpt4 key购买 nike

目标是运行大约 10,000 行链接。确定页码 > 3 的页码并突出显示第一列。我已经完成了所有这些,但问题是 Url Fetch 花费的时间太长,我遇到了最大运行时间错误。无论如何,我可以加快这段代码的速度,以便我可以运行 10,000 行吗?

 function readColumns() {
//program is going to run through column 3 by going through the amount of rows, truncating last three characters to see if pdf, then highlighting first column
var sheet = SpreadsheetApp.getActiveSheet();
var columns = sheet.getDataRange();
var rowNum = columns.getNumRows();
var values = columns.getValues();
var html;
var htmlString;

for(var i = 1; i <= rowNum; i++){
var columnLogger = values[i][2];
try{
html = UrlFetchApp.fetch(values[i][2],
{
muteHttpExceptions: true,
}
);
}catch(e){
Logger.log("Error at line " + i);
var error = true;
}
htmlString = html.getContentText();
var index = htmlString.indexOf("Pages") + 6;
var pageNumber = parseInt(htmlString.charAt(index),10);

var lastChars = "" + columnLogger.charAt(columnLogger.length-3) + columnLogger.charAt(columnLogger.length-2) + columnLogger.charAt(columnLogger.length-1);

if((error) || (!lastChars.equals("pdf") && values[i][6].equals("") && !pageNumber >= 3)){

//goes back to first column and highlights yellow
var cellRange = sheet.getRange(1, 1, rowNum, 3)
var cell = cellRange.getCell(i+1, 1)
cell.setBackground("yellow");
}


}


}

编辑 - 短脚本:

function foreverCall(){
var start = 1480;

for(;;){
readColumns(start);
start = start + 100;
}

}


function readColumns(start) {
//program is going to run through column 3 by going through the amount of rows, truncating last three characters to see if pdf, then highlighting first column
var sheet = SpreadsheetApp.getActiveSheet();
var columns = sheet.getDataRange();
var rowNum = columns.getNumRows();
var values = columns.getValues();
var html;
var htmlString;
var error;

for(var i = start; i < start+100; i++){
if(loop(values, error, html, htmlString, rowNum, sheet, columns, i)){

var cellRange = sheet.getRange(1, 1, rowNum, 3)
var cell = cellRange.getCell(i, 1)
cell.setBackground("yellow");

}
}


}

function loop(values, error, html, htmlString, rowNum, sheet, columns, i){
var columnLogger = values[i][2];


var lastChars = columnLogger.slice(-4);

if(!lastChars.equals(".pdf") && values[i][6].equals("")){


return true;


}else{

try{
error = false
html = UrlFetchApp.fetch(values[i][2].toString());
if(html == null){
error = true;
}
}catch(e){
Logger.log("Error at line " + i);
error = true;
}
if(!error){
htmlString = html.getContentText();
var index = htmlString.indexOf("Pages") + 6;
var pageNumber = parseInt(htmlString.charAt(index),10);

}
//goes back to first column and highlights yellow
if(error || !pageNumber >= 3){
return true;
}
}

return false;

}

最佳答案

您可以替换此:

var lastChars = "" + columnLogger.charAt(columnLogger.length-3) + columnLogger.charAt(columnLogger.length-2) + columnLogger.charAt(columnLogger.length-1);

这样:

var lastChars = columnLogger.slice(-3);

您还可以从 html 侧边栏或对话框启动获取脚本来运行短批处理,然后返回到成功处理程序,该处理程序然后可以根据返回值启动另一个批处理。返回值还可用于在下一行开始下一批。它实际上需要更长的时间来运行,但您可以通过保持较小的批处理来保持在脚本限制以下。

enter image description here

关于javascript - 加速 UrlFetch Google 应用程序脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45664870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com