Google表格脚本-多个getRange循环 [英] Google Sheet Script - Multiple getRange looping

查看:77
本文介绍了Google表格脚本-多个getRange循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个新问题.

到目前为止,我的脚本到目前为止可以在工作表中循环并找到文本"Bank",它将背景色设置为红色,并且将从另一个单元格中获取的值作为标记并记录下来.一旦完成,它将崩溃,因此循环将中断,我不知道为什么?

So far my script here can loop a sheet and find the text "Bank", it will set the background color to red and it will take the value from another cell as marked and log it. Once it have done that once it will crash, so the loop will break, I have no idea why?

function sortBank() { 
      var sheet = SpreadsheetApp.getActiveSheet(); 
      var rows = sheet.getDataRange(); 
      var numRows = rows.getNumRows(); 
      var values = rows.getValues(); 
      var rowsDeleted = 0

      for (var i = 0; i <= numRows - 1; i++) { 
        var row = values[i]; 
        if (row[8].indexOf('Bank') > -1) { /** Set the Job prefix **/

          sheet.getRange(parseInt(i)+1,9).setBackgroundColor("#f44336");
          var values = sheet.getRange(parseInt(i),2).getValues();

          Logger.log(values[0][0]);

        } 
      } 
    };

推荐答案

您可能会看到类似的问题:

You may look at similar questions:

Google脚本-15秒后出现内部错误

Google脚本-超过了最大执行时间,帮助优化

基本解决方案是使用getValues()一次,然后在2d数组中循环值:

Basic solution is to use getValues() one time and then loop values in 2d array:

var sheet = SpreadsheetApp.getActiveSheet(); 
var rows = sheet.getDataRange(); 
var data = rows.getValues();

for (var i = 0; i < numRows; i++)
{
  var j = SomeValue; // column number - 1
  var row = data[i]; // row from origonal data range
  var value = row[j]; // value from data
  // some other code...   
} 

此处:

如果您找到最小化脚本的方法,您的脚本将运行得更快. 调用脚本对这些服务进行的操作.

Your scripts will run faster if you can find ways to minimize the calls the scripts make to those services.

这篇关于Google表格脚本-多个getRange循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆