简体   繁体   English

如何使for循环脚本更有效?

[英]How to make for loop script more efficient?

I'm very new to script writing in general, especially in GAS, and want to make sure I learn good habits. 一般来说,我对脚本编写非常陌生,尤其是在GAS中,并且想确保自己养成良好的习惯。 I wrote a for loop script that, in essence, does the following: 我写了一个for循环脚本,实质上,它执行以下操作:

  1. Read through column A starting at row 2 从第2行开始通读A列
  2. If cell above current cell in for loop is the same value, then clear the contents of the adjacent cell to the right of the current cell. 如果for循环中当前单元格上方的单元格具有相同的值,则清除当前单元格右侧的相邻单元格的内容。

For example 例如

Cell A1 contains "Something". 单元格A1包含“某些内容”。 Cell B1 contains "Exciting" 单元格B1包含“令人兴奋”

Cell A2 contains "New". 单元格A2包含“新建”。 Cell B2 contains "X" 单元格B2包含“ X”

Cell A3 contains "New". 单元格A3包含“新建”。 Cell B3 contains "Y" 单元格B3包含“ Y”

Since A3 has the same value as cell A2 (that value being "New"), cell B3 (value currently "Y") is cleared so that there is no value in cell B3. 由于A3具有与单元格A2相同的值(该值为“ New”),因此清除了单元格B3(当前值为“ Y”),因此单元格B3中没有值。

It seems to take a very long time to run, which I am sure is due to my novice writing, and I want to make this script as efficient as possible. 运行时间似乎很长,我确信这是由于我是新手编写的,所以我想使此脚本尽可能高效。

Do any of you scripting gods have any suggestions to make this script more efficient? 你们中的任何脚本专家有什么建议可以提高此脚本的效率吗?

I would also appreciate any explanation as to why any suggestion would be more efficient for my own understanding, and so that anyone that happens to find this posting, later on, can understand it as well. 我也希望对为什么任何建议对我自己的理解更有效率的解释,以便以后碰巧找到此帖子的任何人也能理解。

Here is the script: 这是脚本:

function testCode() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Test 1");
 var source = sheet.getRange("A2:A");

 for (i = 2; i < source.getLastRow(); i++) {
 var currentCell = sheet.getRange(i,1).getValue();
 var cellAbove = sheet.getRange(i-1,1).getValue();
 var cellRight = sheet.getRange(i,2);
 if (currentCell == cellAbove){
 cellRight.clearContent(),({contentsOnly:true});
  }
 }
}

THANK YOU 谢谢

The biggest issue is that you are getting three new ranges in every loop. 最大的问题是,您在每个循环中都会获得三个新范围。 There is a way to do this without getting new ranges in the loop. 有一种方法可以在不获取新范围的情况下执行此操作。 You'll need to get the data in both columns A and B. 您需要同时在A和B列中获取数据。

function testCode() {
  var cellAbove,currentCell,cellRight,data,lastRow,L,sheet,source,ss;

  ss = SpreadsheetApp.getActiveSpreadsheet();
  sheet = ss.getSheetByName("Test 1");
  lastRow = sheet.getLastRow();

  source = sheet.getRange(2,1,lastRow-1,2);//Get data starting in row 2 and column 1
  //Get the number of rows that are the lastRow minus the number of rows not included 
  //at the top - get 2 columns

  data = source.getValues();//Get a 2D array of values
  L = data.length;//Get the number of elements in the outer array- which is the number of
  //rows in the array

  for (var i = 0; i < L; i++) {

    if (i == 0) {continue;} //If this is the first loop there is no value above it to check

    currentCell = data[i][0];
    cellAbove = data[i-1][0];

    if (currentCell == cellAbove){
      cellRight = sheet.getRange(i+1,2);
      cellRight.clearContent(),({contentsOnly:true});
    }
  }
}

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

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