Containerhouse
Containerhouse

Reputation: 3

How to change multiple cell values in Google Sheets via Apps Script?

I want to write a script to format a production schedule for printing. I want to enter single letters, then run the script to change those single letters into words and format the cells with borders, etc.

The following if/ else if/ else operation does what I want for a single cell:

    var TESTalpha = printCHSheet.getRange(4, 5, 1, 1);
    var TESTalphaData = TESTalpha.getValue();

    if (TESTalphaData == 'f') {
    TESTalpha.clear();
    TESTalpha.setValue('FAB').setBorder(true, true, true, true, true, true);
    } else if (TESTalphaData == 'p') {
    TESTalpha.clear();
    TESTalpha.setValue('PAINT').setBorder(true, true, true, true, true, true);
    } else if (TESTalphaData == 'c') {
    TESTalpha.clear();
    TESTalpha.setValue('FINISH').setBorder(true, true, true, true, true, true);
    } else {
    TESTalpha.setValue(null);
    }

Eventually I want to apply this to a range of cells in rows and columns, but first I am trying to evaluate five cells on a single row.

The following loop runs, but replaces every cell with the last value it finds (FYI...the five cells E4:I4 are populated with f/ f/ p/ c/ c):

    var TESTalpha = printCHSheet.getRange(4, 5, 1, 5);
    var TESTlength = TESTalpha.getLastColumn();
    var TESTalphaData = TESTalpha.getValues();

    for (var i=0; i < TESTlength+1; i++) {
    if (TESTalphaData[0][i] == "f") {
    TESTalpha.clear();
    TESTalpha.setValue("FAB");
    } else if (TESTalphaData[0][i] == "p") {
    TESTalpha.clear();
    TESTalpha.setValue("PAINT");
    } else if (TESTalphaData[0][i] == "c") {
    TESTalpha.clear();
    TESTalpha.setValue("FINISH");
    }
    }

The result I get here is that all cells in range E4:I4 are set to FAB; then all set to PAINT; then all set to FINISH. What I want to have, rather, is FAB/ FAB/ PAINT/ FINISH/ FINISH. I know the above script is replacing all the cells because I'm programming it to, but my various modifications have failed, so I post it here as possibly heading in the right direction.

Another idea I had is to run a loop to splice the array, then clear the range and set (or push) the new array modified by the loop:

    var TESTalpha = spreadsheet.getRange("E4:I4");
    var TESTlength = TESTalpha.getLastColumn();

    for (var i=0; i < TESTlength+1; i++) {

    var TESTalphaData = TESTalpha.getValues();

    if (TESTalphaData[0][i] == "f") {
    TESTalphaData.splice([i], 1, "FAB");
    } else if (TESTalphaData[0][i] == "p") {
    TESTalphaData.splice([i], 1, "PAINT");
    } else if (TESTalphaData[0][i] == "c") {
    TESTalphaData.splice([i], 1, "FINISH");
    } 

    spreadsheet.getRange("E4:I4").clear();
    spreadsheet.getRange("E4:I4").setValues(TESTalphaData);

    }

The problem with the above is (at least) that it's a 2D array, yet I'm trying to splice like a 1D array of strings.

I've tried a switch, too, without success.

Thanks in advance for any assistance!

Upvotes: 0

Views: 8602

Answers (3)

Containerhouse
Containerhouse

Reputation: 3

This does it! A for loop inside a for loop to set borders if it's false that the cell's blank--In other words, if it's populated.

for (var r = 4; r < 50; r++) {

  for (var c = 5; c < 21; c++) {

       var singleCell = printCHSheet.getRange(r, c, 1, 1);
       var singleCellBlank = singleCell.isBlank();

     if ( !singleCellBlank ) {
                  singleCell.setBorder(true, true, true, true, true, true)
     }
  }
}

I'm elated. Now all I've got to do is format some fonts, add other letters to the else if evaluations, etc., and I'm done!

Upvotes: 0

Containerhouse
Containerhouse

Reputation: 3

Partial answer (still need help on putting borders around only the cells that contain a value). This while loop inside of another while changes values of the multiple dimensional array consisting of rows and columns. Many thanks to @Kriggs.

var TESTalpha = printCHSheet.getRange(4, 5, 7, 7),
    TESTalphaData = TESTalpha.getValues(),
    rowOp = TESTalpha.getNumRows();

while ( --rowOp >= 0 ) {

 var col = TESTalphaData[rowOp].length;

   while( --col >= 0 ) {

      if (TESTalphaData[rowOp][col] == "f") {
          TESTalphaData[rowOp][col] = "FAB";
      } else if (TESTalphaData[rowOp][col] == "p") {
                 TESTalphaData[rowOp][col] = "PAINT";
      } else if (TESTalphaData[rowOp][col] == "c") {
                 TESTalphaData[rowOp][col] = "FINISH";
      }

   }

}

TESTalpha.setValues(TESTalphaData);

The thing is, TESTalphaData is an array, so I can't set borders with it. I can set borders with TESTalpha (b/c it's a range), but then I get borders around exactly 7 cells per row (as I defined the range).

How can I run another loop to do something like this (which doesn't work)? if (rangeX != null) {rangeX.setBorder(true, true, true, true, true, true); Again, a range won't check values, but I can't set borders for values, since they exist in arrays apart from cells.

Really appreciate any help!

Upvotes: 0

Kriggs
Kriggs

Reputation: 3778

Don't just copy and use it, it's commented so you know what to do next time:

var TESTalpha = spreadsheet.getRange("E4:I4"),
    TESTalphaData = TESTalpha.getValues();

var col = TESTalphaData[0].length; // use a meaningful name for a variable
while( --col >= 0 ) {
// Make the tests and assign the variable the array you pulled from the sheet with getValues(), it's exactly the same you need to input with setValues()
if (TESTalphaData[0][col ] == "f") {
   TESTalphaData[0][col] = 'FAB';
} else if (TESTalphaData[0][col] == "p") {
   TESTalphaData[0][col] = "PAINT";
} else if (TESTalphaData[0][col] == "c") {
   TESTalphaData[0][col] = "FINISH";
}
// Setting the values should be after the loop is complete, so I removed it from inside the for (now a while), to the end of the code
}
// No need to get the Ranges again, you already save it up there
// No need to clear the range, it will all be re-written again
TESTalpha.setValues(TESTalphaData);

Upvotes: 1

Related Questions