I have a code, where I apply the same rules for more than two elements:
activities.forEach((act) => {
mainSheet.getRange(rowIndex, columnIndex)
.setValue(act.text)
.setFontStyle('italic')
.setFontFamily('Verdana')
.setFontSize(12)
.setBorder(null, null, true, null, null, null, act.color, SpreadsheetApp.BorderStyle.SOLID_MEDIUM)
mainSheet.getRange(rowIndex, columnIndex 2)
.setFontStyle('italic')
.setFontFamily('Verdana')
.setFontSize(10)
.setBorder(null, null, true, null, null, null, act.color, SpreadsheetApp.BorderStyle.SOLID_MEDIUM)
rowIndex = 2;
})
I repeat:
FontStyle
FontFamily
setBorder
Any suggestions on how I can make it neater?
I would like to use copy format in Google Sheets, but I don't want to make a cell for it. Maybe it's possible to create a not existing cell, so I can write my formatting there and then apply it to my elements?
CodePudding user response:
You can set the value and format of all cells with one call by creating a RangeList
object. To create the RangeList
object, use Array.map()
or another Array
method to collect the ranges, like this:
const rowNumbers = [2, 4, 6, 8, 10, 12,];
const columnLabels = ['C', 'E'];
const rangeList = rowNumbers.map(row => columnLabels.map(col => col row));
mainSheet.getRangeList(rangeList)
.setValue(act.text)
.setFontStyle('italic')
.setFontFamily('Verdana')
.setFontSize(12)
.setBorder(null, null, true, null, null, null, act.color, SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
It is unclear why you are looping over activities.forEach()
if your intention is to use the same format with all cells. If you want to only set the value in some of the cells and not all of them, create another RangeList
object and call its .setValue()
method.