简体   繁体   English

使用GAS将值添加到所选单元格区域的单元格中

[英]Adding value to a cell to the selected range of cells using GAS

I need to add the passed value to the function (say 20.35) to every selected cells. 我需要将传递的值添加到函数(例如20.35)中的每个选定单元格中。 I have a huge list and the function below times out as far it can run upto 6 minutes without getting the added result to cells 我有一个巨大的列表,下面的功能超时,它最多可以运行6分钟而不会将结果添加到单元格中

How do I achive a workaround to write the cell everytime soon after completing the computation one after the other. 在一个接一个地完成计算之后,我如何尽快找到一种方法来每次写入单元格。 For example if it expires after completing 10260 then upto 10259 the cell values are added with 20.35 which I can highlight by changing the color of cell at 1 to 10259. Those cells which original color means they arent added. 例如,如果它在完成10260之后到期,则最多增加10259,将为单元格值添加20.35,我可以通过将单元格的颜色更改为1到10259来突出显示这些单元格。原始颜色表示它们未添加。

 function addToCells(isAll, valueToAdd) {
     var sheet = SpreadsheetApp.getActiveSheet();
     if (isAll)
         var range = sheet.getDataRange();
     else
         var range = sheet.getActiveRange();

     var Values = range.getValues();

     for (var i in Values) {
         for (var j in Values[i]) {
             var value = Values[i][j].toString();
             if (value) {
                 Values[i][j] = parseFloat(value) + valueToAdd;
                 //how to write Values[i][j] to cell now
             }
         }
     }
     range.setValues(Values); // this will not execute if timesout

 }

Your script is converting numbers to strings and back, which takes a lot more time than adding 1 to a number. 您的脚本正在将数字转换为字符串然后返回,这比将数字加1要花费更多的时间。 I understand you want to avoid messing up strings with "1" at the end, and you probably don't want all empty cells to get "1" in them. 我知道您想避免在结尾处弄乱字符串“ 1”,并且您可能不希望所有空单元格中的字符都为“ 1”。 Both are achieved by checking the type of an element before adding to it: 两者都是通过在添加元素之前检查元素的类型来实现的:

function testadd() {
  var range = ... // whatever
  var values = range.getValues();
  var newValues = values.map(function(row) {
    return row.map(function(elem) {
      return (typeof elem == "number" ? elem + 1 : elem);
    });
  });
  range.setValues(newValues);
}

You do not want to run separate "setValue" for each cell, that would be a real performance killer and will likely leave you out of execution time quota pretty soon. 不想单独运行“的setValue”每个细胞,这将是一个真正的性能杀手,很可能会很快离开你了执行时间配额。

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

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