I am looking to how you can pass arguments into a nested anonymous map function. So in a function like the below, the values of red, green, blue in the nested map are hardcoded. How can parameters be passed into the nested map?
function myFunction() {
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
const grid = { sheetId: 0, startRow: 0, startCol: 0 }; // Please set your gridrange.
const values = [["sample value1", "sample value2"], ["sample value3", "sample value4"]]; // Please set your values as 2 dimensional array.
const request = [{
updateCells: {
range: {
sheetId: grid.sheetId,
startRowIndex: grid.startRow,
startColumnIndex: grid.startCol,
},
rows: values.map(r => ({
values: r.map(c => ({
** userEnteredFormat: { backgroundColor: { red: 1, green: 0.4, blue: 0.4 } }**,
userEnteredValue: { stringValue: c }
}))
})),
fields: "userEnteredFormat,userEnteredValue"
}
}];
Sheets.Spreadsheets.batchUpdate({ requests: request }, spreadsheetId);
}
Thank you in advance
CodePudding user response:
In your situation, how about the following modification?
Modified script:
function myFunctionb() {
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
const grid = { sheetId: 0, startRow: 0, startCol: 0 }; // Please set your gridrange.
const values = [["sample value1", "sample value2"], ["sample value3", "sample value4"]]; // Please set your values as 2 dimensional array.
const backgroundColors = [[[1, 0.4, 0.4], [0.4, 1, 0.4]], [[0.4, 0.4, 1], [0.4, 0.4, 0.4]]]; // Please set your background colors as 2 dimensional array.
const request = [{
updateCells: {
range: {
sheetId: grid.sheetId,
startRowIndex: grid.startRow,
startColumnIndex: grid.startCol,
},
rows: values.map((r, i) => ({
values: r.map((c, j) => ({
userEnteredFormat: { backgroundColor: { red: backgroundColors[i][j][0], green: backgroundColors[i][j][1], blue: backgroundColors[i][j][2] } },
userEnteredValue: { stringValue: c }
}))
})),
fields: "userEnteredFormat,userEnteredValue"
}
}];
Sheets.Spreadsheets.batchUpdate({ requests: request }, spreadsheetId);
}
- When this script is run, 4 values are put from cell "A1" and also 4 different background colors are set from cell "A1".