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!
TESTalphaData[0][i] = 'FAB',. - KriggsTESTalphaData[0][i].push("FAB");but that doesn't work--I think b/c push wants an integer. - Containerhouse