This is how I would do this. It is a bit long but I think it's pretty pragmatic and reusable. Definitely functional.
This uses the V8 Engine and TypeScript
/*
Transforms the original "Array of Arrays"—
[
[a, b, c, d, e],
[a, b, c, d, e],
[...],
...,
]
into an "Array of Objects".
[
{timestamp: a, email: b, response_1: c, response_2: d, response_3: e},
{timestamp: a, email: b, response_1: c, response_2: d, response_3: e},
{...},
...,
]
*/
var original_values = SpreadsheetApp.getActiveSheet()
.getRange("A:E")
.getValues()
.map(
([
a, b, c, d, e,
// f, g, h, i, j,
// k, l, m, n, o,
// p, q, r, s, t,
// u, v, w, x, y,
// z, aa, ab, ac, ad,
// etc...
]) => {
return Object.create({
timestamp: a,
email: b,
response_1: c,
response_2: d,
response_3: e,
});
}
);
/*
Appends the string to some part of the Objects in our Array.
Since the Objects match the table structure (hopefully) we can be
pretty specific.
I tried to mock how a Google Form might collect responses.
*/
var appended_string = original_values.map(
(arg: { timestamp; email; response_1; response_2; response_3 }) => {
switch (typeof arg.response_1) {
case "string":
return Object.assign(arg, {
response_1: (arg.response_1 += " string"),
});
default:
return arg;
}
}
);
/*
Need to reshape the "Array of Objects" back into an "Array of Arrays".
Pretty simple compared to the original.
*/
var values_to_set = appended_string.map(
(arg: { timestamp; email; response_1; response_2; response_3 }) => {
return [
arg.timestamp,
arg.email,
arg.response_1,
arg.response_2,
arg.response_3,
];
}
);
/*
Here we'll take our finalized "values_to_set Array of Arrays" and
use it as the input for ".setValues()".
All Google Sheets data starts and ends as an "Array of Arrays" but...
It is significantly easier to work with as an "Array of Objects".
Rhetorical Question: Who wants to keep track of indexes?
*/
SpreadsheetApp.getActiveSheet().getRange("A:E").setValues(values_to_set);