Google apps script 我可以更改代码以使脚本运行更快吗?
我想找出两件事Google apps script 我可以更改代码以使脚本运行更快吗?,google-apps-script,Google Apps Script,我想找出两件事 我可以优化我的代码以使其运行更快吗 有没有办法测试我脚本的速度 我的脚本只是从URL(例如:)中提取数据,该URL将粘贴到电子表格的第一个单元格中。然后,它用存储标题的第一列和存储摘要的第二列填充单元格 我使用的触发器将执行脚本onEdit(),这样每当对电子表格的第一个单元格进行编辑时,它都会运行 任何提示,提示,甚至是一个去看这里是感激的 这是我的密码 function respondToSearch() { // Gets the active sheet in
function respondToSearch() {
// Gets the active sheet in a spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Gets sheet 1 and sets the first column to a width of 200 and the second
column to width 500
var sheet = ss.getSheets()[0].setColumnWidth(1, 200).setColumnWidth(2, 500);
var activeSheet = SpreadsheetApp.getActiveSheet();
// Gets the value from the top left cell in a range
var dataRange = activeSheet.getDataRange().getValue();
// Sends an HTTP request to fetch the URL
var searchResponse = UrlFetchApp.fetch(dataRange);
// Get the response as a string and parse (string is in JSON format)
var parsedResponse = Utilities.jsonParse(searchResponse.getContentText());
// Array to hold letter corresponding to spreadsheet columns
var cellLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t"];
// Loop through the "item" objects and display the properties in the
spreadsheet
for (var i = 0; i < 2; i++) {
for (var j = 0; j < parsedResponse.items.length; j++) {
// Starts with items[0] (1st object) and displays the properties
var parsedItems = [parsedResponse.items[j].title, parsedResponse.items[j]
.summary];
// If a property is not undefined print the property in the spreadsheet
else print "N/A"
if (parsedItems[i] != undefined) {
var print = parsedItems[i];
}
else {
var print = "N/A";
}
// Stores the current letter corresponding to the current spreadsheet column
var cellLtr = cellLetters[i];
// Starts the row available to display data at number 2
var cellNum = [j + 2];
var cell = cellLtr + cellNum;
sheet.setActiveCell(cell).setFontSize("9").setHorizontalAlignment("left")
.setVerticalAlignment("top").setValue(print);
}
}
}
函数respondToSearch(){
//获取电子表格中的活动工作表
var ss=SpreadsheetApp.getActiveSpreadsheet();
//获取工作表1,并将第一列的宽度设置为200,第二列的宽度设置为200
列宽为500
var sheet=ss.getSheets()[0].setColumnWidth(1200).setColumnWidth(2500);
var activeSheet=SpreadsheetApp.getActiveSheet();
//从范围中的左上角单元格获取值
var dataRange=activeSheet.getDataRange().getValue();
//发送HTTP请求以获取URL
var searchResponse=UrlFetchApp.fetch(数据范围);
//以字符串形式获取响应并进行解析(字符串为JSON格式)
var parsedResponse=Utilities.jsonParse(searchResponse.getContentText());
//用于保存与电子表格列对应的字母的数组
变量cellLetters=[“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”,
“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”];
//循环遍历“item”对象并在
电子表格
对于(var i=0;i<2;i++){
对于(var j=0;j
多谢各位
我可以优化我的代码以使其运行更快吗
我认为这是可能的。尝试重写代码,设置字体大小和对齐方式,不是针对每个单元格,而是针对整个范围
有没有办法测试我脚本的速度
对。代码应该如下所示
var start = new Date();
my_time_consuming_function();
var diff = new Date() - start;
Logger.log("my_time_consuming_function takes: " + diff + " ms");
我可以优化我的代码以使其运行更快吗
我认为这是可能的。尝试重写代码,设置字体大小和对齐方式,不是针对每个单元格,而是针对整个范围
有没有办法测试我脚本的速度
对。代码应该如下所示
var start = new Date();
my_time_consuming_function();
var diff = new Date() - start;
Logger.log("my_time_consuming_function takes: " + diff + " ms");
您可以避免在
j
的每次迭代中查找和创建parsedItems[i]
以及更改cellLtr
for (var i = 0; i < 2; i++) {
var cellLtr = cellLetters[i];
for (var j = 0; j < parsedResponse.items.length; j++) {
var print = (i == 0 ? parsedResponse.items[j].title : parsedResponse.items[j].summary);
if (print == undefined)
print = "N/A";
var cell = cellLtr + (j + 2);
sheet.setActiveCell(cell).setFontSize("9").setHorizontalAlignment("left")
.setVerticalAlignment("top").setValue(print);
}
}
for(变量i=0;i<2;i++){
var cellLtr=细胞字母[i];
for(var j=0;j
您可以避免在j
的每次迭代中查找和创建parsedItems[i]
以及更改cellLtr
for (var i = 0; i < 2; i++) {
var cellLtr = cellLetters[i];
for (var j = 0; j < parsedResponse.items.length; j++) {
var print = (i == 0 ? parsedResponse.items[j].title : parsedResponse.items[j].summary);
if (print == undefined)
print = "N/A";
var cell = cellLtr + (j + 2);
sheet.setActiveCell(cell).setFontSize("9").setHorizontalAlignment("left")
.setVerticalAlignment("top").setValue(print);
}
}
for(变量i=0;i<2;i++){
var cellLtr=细胞字母[i];
for(var j=0;j
我不确定JSPerf是否有助于评估Google Aps脚本的性能,因为JSPerf衡量的是Web浏览器Javascript引擎的性能,而Google应用程序脚本正在Google服务器上运行。您是否尝试过从Google Apps脚本启动JSPerf?@megabyte1024您是对的。我编辑了我的答案。我最初的意思是,他可以用它来发现不是由调用应用程序API造成的瓶颈。但这并不可靠,因为谷歌服务器可以(而且可能确实)进行浏览器无法进行的优化,所以我想我只是不假思索地编写了它。不幸的是,GAS现在效率不高。我不确定JSPerf是否有助于评估Google Aps脚本的性能,因为JSPerf衡量的是Web浏览器Javascript引擎的性能,而Google应用程序脚本正在Google服务器上运行。您是否尝试过从Google Apps脚本启动JSPerf?@megabyte1024您是对的。我编辑了我的答案。我最初的意思是,他可以用它来发现不是由调用应用程序API造成的瓶颈。但这并不可靠,因为谷歌服务器可以(而且可能确实)进行浏览器无法进行的优化,所以我想我只是不假思索地编写了它。不幸的是,GAS现在效率不高。请看我的。可能是愚蠢的问题,但是,我应该把代码(速度测试)放在我的其他代码的开头还是结尾??我的\u耗时的\u函数()未定义?我应该开始吗