Iterate over range, append string to each
Solution 1:
You can try something like this:
//
// helper `forEachRangeCell` function
//
function forEachRangeCell(range, f) {
const numRows = range.getNumRows();
const numCols = range.getNumColumns();
for (let i = 1; i <= numCols; i++) {
for (let j = 1; j <= numRows; j++) {
const cell = range.getCell(j, i)
f(cell)
}
}
}
//
// Usage
//
const range = SpreadsheetApp.getActiveSheet().getActiveRange();
forEachRangeCell(range, (cell) => {
cell.setValue(`${cell.getValue()} string`)
})
Solution 2:
Or alternatively use setValues() which writes the all values at the same time. Seems to execute quicker too.
var range = SpreadsheetApp.getActiveSheet().getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var writeValues = []
for (var i = 1; i <= numRows; i++) {
var row = []
for (var j = 1; j <= numCols; j++) {
var currentValue = range.getCell(i,j).getValue();
var withString = currentValue + " string";
row.push(withString)
}
writeValues.push(row)
}
range.setValues(writeValues)