简体   繁体   English

循环 Google 表格脚本

[英]Loops Google Sheets Script

The problem I'm trying to solve is the following.我试图解决的问题如下。 I'm trying to make a loop which finds the letter "Y" and only the Y. I want to make it so if a cell has Y in it, it will make the 4 rows to the left of it be called %NULL%.我正在尝试创建一个循环,找到字母“Y”并且只有 Y。我想这样做,所以如果一个单元格中有 Y,它将使其左侧的 4 行称为 %NULL% . The issue is there are 2 cells per row where Y could be.问题是每行有 2 个单元格 Y 可能是。

This is the code I started but don't know where to go from.这是我开始的代码,但不知道 go 从哪里来。

function Loop1(){

  var sheet = SpreadsheetApp.getActive().getSheetByName("EXP50");
  var data = sheet.getDataRange().getValues();

  for(var i = 1; i< data.length; i++){
      if(data[i][7-1] === "Y"){
        sheet.getRange(i + 1, 3).setValue("%NULL%");
        sheet.getRange(i + 1, 4).setValue("%NULL%");
        sheet.getRange(i + 1, 5).setValue("%NULL%");
        sheet.getRange(i + 1, 6).setValue("%NULL%");
      }
      if(data[i][13-1] === "Y") {
        sheet.getRange(i + 1, 9).setValue("%NULL%");
        sheet.getRange(i + 1, 10).setValue("%NULL%");
        sheet.getRange(i + 1, 11).setValue("%NULL%");
        sheet.getRange(i + 1, 12).setValue("%NULL%");        
      }

  }

}

Instead of a if loop, couldn't you use something like this:而不是if循环,你不能使用这样的东西:

function Loop1(){

  var sheet = SpreadsheetApp.getActive().getSheetByName("EXP50");
  var data = sheet.getDataRange().getValues();

  while 1 = 1:
      if(data[i][7-1] === "Y"){
        sheet.getRange(i + 1, 3).setValue("%NULL%");
        sheet.getRange(i + 1, 4).setValue("%NULL%");
        sheet.getRange(i + 1, 5).setValue("%NULL%");
        sheet.getRange(i + 1, 6).setValue("%NULL%");
      }
      if(data[i][13-1] === "Y") {
        sheet.getRange(i + 1, 9).setValue("%NULL%");
        sheet.getRange(i + 1, 10).setValue("%NULL%");
        sheet.getRange(i + 1, 11).setValue("%NULL%");
        sheet.getRange(i + 1, 12).setValue("%NULL%");        
      }

  }

}

(Apologies, I do not know if this will resolve your issue.) (抱歉,我不知道这是否能解决您的问题。)

I believe your goal is as follows.我相信你的目标如下。

  • You want to check the columns "G" and "M".您要检查“G”和“M”列。
  • When the cell value of the columns is "Y", you want to put %NULL% to the columns "C" - "F" and the columns "I" - "L" in the same row.当列的单元格值为“Y”时,您希望将%NULL%放入同一行中的“C”-“F”列和“I”-“L”列。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本实现此目的。

Modification points:修改点:

  • I thought that setValue is used in the loop, the process cost will become high.本以为循环使用setValue ,流程成本会变高。

In this answer, by reducing the process cost, I would like to propose the following 2 patterns.在这个答案中,通过降低流程成本,我想提出以下两种模式。

Pattern 1:模式一:

In this pattern, the methods of getValues and setValues are used.在此模式中,使用了getValuessetValues方法。

function Loop1() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("EXP50");
  var range = sheet.getDataRange();
  var data = range.getValues();
  data.forEach(r => {
    var [g, m] = [r[6], r[12]];
    var temp = Array(4).fill("%NULL%");
    if (g == "Y") r.splice(2, 4, ...temp);
    if (m == "Y") r.splice(8, 4, ...temp);
  });
  range.setValues(data);
}

Pattern 2:模式二:

In this pattern, TextFinder is used.在此模式中,使用了 TextFinder。

function Loop1() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("EXP50");
  var ranges = sheet.createTextFinder("Y").matchCase(true).matchEntireCell(true).findAll().reduce((ar, r) => {
    var col = r.getColumn();
    var row = r.getRow();
    if (col == 7) ar.push(`C${row}:F${row}`);
    if (col == 13) ar.push(`I${row}:L${row}`);
    return ar;
  }, []);
  sheet.getRangeList(ranges).setValue("%NULL%");
}

References:参考:

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

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