简体   繁体   English

对多个单元格使用 If 函数

[英]Using If function for multiple cells

Hi I have a working program to pull data from one sheet to another sheet and place in the relevant cells, however with one column I want to change the results which reads a "K" from the source sheet and write an "A" to the target sheet using a if function but I can only seem to do one cell and not the full column.嗨,我有一个工作程序可以将数据从一张工作表拉到另一张工作表并放置在相关单元格中,但是对于一列,我想更改从源工作表中读取“K”并将“A”写入到使用 if 函数的目标表,但我似乎只能做一个单元格而不是整列。 How can I do this for multiple cells?如何为多个单元格执行此操作? If I try.getRange("F18:41") it doesn't seem to work.如果我 try.getRange("F18:41") 它似乎不起作用。 Please see the code below:请看下面的代码:

      var sss = SpreadsheetApp.openById('....'); // sss = source spreadsheet
      var ss = sss.getSheetByName('.....); // ss = source sheet

    //Get full range of data sheet 1

    var TypeWire = ss.getRange("F18");

    //get A1 notation identifying the range

    var A16Range = TypeWire.getA1Notation();

    //get the data values in range  

    var SDataSixteen = TypeWire.getValues();

     var tss = SpreadsheetApp.openById('.....'); // tss = target spreadsheet
     var ts = tss.getSheetByName('....'); // ts = target sheet

    //set the target range to the values of the source data
    ts.getRange("C40:C61").setValues(SDataSeven);


    if (SDataSixteen == "K")

    {
    ts.getRange("L16").setValue("A");
    } 
}

The getValues() method of Class Range returns a multidimentional (2D) array. Class Range 的getValues()方法返回一个多维 (2D) 数组。 The elements of the "outer" array are arrays that represent rows. “外部”数组的元素是表示行的数组。 The elements of the inner arrays are objects that represent the cell values for the corresponding row.内部数组的元素是表示相应行的单元格值的对象。

There are several ways to do what you are looking for.有几种方法可以做你正在寻找的东西。 Perhaps the easier to understand is the technique shown in the Cooper's answer: Use nested for statements.也许更容易理解的是 Cooper 的回答中显示的技术:使用嵌套的 for 语句。

Use one for statement to iterate over the rows, then another for loop to iterate over the row cells.使用一个 for 语句遍历行,然后使用另一个 for 循环遍历行单元格。

IMPORTANT NOTE:重要的提示:

Using loops to write single cells values is very slow.使用循环写入单个单元格值非常慢。 For recommendations please read https://developers.google.com/apps-script/guides/support/best-practices有关建议,请阅读https://developers.google.com/apps-script/guides/support/best-practices

Related有关的

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

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