0

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");
    } 
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
OWEN
  • 1
  • SDataSeven is undefined – Cooper Jan 05 '20 at 20:59
  • `var TypeWire=ss.getRange("F18");//only getting one value` `var A16Range=TypeWire.getA1Notation();//A16Range not used at all` `var SDataSixteen=TypeWire.getValue();//getValue is a more sensible choice since TypeWire is only one cell` – Cooper Jan 05 '20 at 21:05
  • If you don't use Excel, don't tag with Excel. This question has nothing whatsoever to do with Excel. – teylyn Jan 05 '20 at 21:22

1 Answers1

1

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

Rubén
  • 34,714
  • 9
  • 70
  • 166