简体   繁体   中英

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. How can I do this for multiple cells? If I try.getRange("F18:41") it doesn't seem to work. 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. 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.

Use one for statement to iterate over the rows, then another for loop to iterate over the row cells.

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

Related

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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