简体   繁体   English

无能为力-超过执行时间

[英]Clueless - Exceeding execution time

I took this code from a sheet that was provided to me to use, but it's inefficient and times out before completing. 我从提供给我使用的工作表中获取了此代码,但是效率低下并且在完成之前会超时。 I'm trying HARD to re-write it in a super simple format to get it to complete within the time limit. 我正在尝试HARD以超简单的格式重写它,以使其在时限内完成。 This is what I started with: 这是我开始的:

    function addTo() {
var ss = SpreadsheetApp.getActiveSheet();
// Style 1
  // Size 2
  var num1 = ss.getRange("AL2").getValue();
  var num2 = ss.getRange("O2").getValue();
  ss.getRange("O2").setValue(num1+num2);
  ss.getRange("AL2").clearContent();
  // Size 4  
  var num1 = ss.getRange("AM2").getValue();
  var num2 = ss.getRange("P2").getValue();
  ss.getRange("P2").setValue(num1+num2);
  ss.getRange("AM2").clearContent();

Then repeat that about 200 times. 然后重复大约200次。 Someone suggested processing as a range instead. 有人建议将其作为范围进行处理。 That makes sense and should result in a faster process. 这是有道理的,应该会加快流程。 But I'm having trouble writing it. 但我在编写它时遇到了麻烦。 This is what I have so far (it's not working though): 这是我到目前为止(虽然没有用):

function addallatOnce() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var startrange = ss.getRange("Inventory Received!B2:U48");
  var addrange =  ss.getRange("Inventory Received!Y2:AR48");
  var complete = startrange+addrange
  ss.getRange("Inventory Received!B50:U96").setValues(complete);
}

I don't usually write code, I can understand what I'm reading, but I don't know anything about writing it. 我通常不编写代码,我可以理解所读内容,但是对编写它一无所知。 Help would be greatly appreciated. 帮助将不胜感激。 Thank you in advance. 先感谢您。

With Google Script 使用Google脚本

Google has some excellent documentation on optimizing code. Google在优化代码方面有一些出色的文档 In this case you can perform batch operations using arrays. 在这种情况下,您可以使用数组执行批处理操作。 First feed the spreadsheet data into the arrays using the getValues() function on the ranges. 首先使用范围上的getValues()函数将电子表格数据输入数组。 Note that getValues() returns a 2D array of the values within the range. 请注意,getValues()返回范围内值的二维数组。 Loop through all the values and add them together, value by value. 遍历所有值并将它们逐个值相加。 Then assign the desired range the value of the complete array. 然后,为所需范围分配整个数组的值。

 function addallatOnce() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var startrange = ss.getRange("Inventory Received!B2:U48").getValues(); var addrange = ss.getRange("Inventory Received!Y2:AR48").getValues(); var complete = addrange; var row = 0; var col = 0; for (row = 0; row < complete.length; row++) { for (col = 0; col < complete[0].length; col++) { complete[row][col] = addrange[row][col] + startrange[row][col]; } } ss.getRange("Inventory Received!B50:U96").setValues(complete); } 

Without Google Script 没有Google脚本

Put the following formula in cell B50 of Inventory Received: 将以下公式放在“收到的库存”的单元格B50中:

=ARRAYFORMULA(B2:U48+Y2:AR48)

See documentation here . 请参阅此处的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM