โญapp.makeTable()
GAS โฉ app โฉ members โฉ .makeTable()
๐พ google apps script
โฌ๏ธ ้่ฆ๏ผapp.range.prototype, app.color
// ๐ธ app.makeTable
app.makeTable = function(sheetName, data, {
// internal parameters for `app.makeTable` function
sheet = app.sheetByName(sheetName),
rows = data.length,
cols = data[0].length,
dataRange = sheet.range(1,1,rows, cols),
// โญ ๆฌไฝๅฏฌๅบฆ๏ผ(dataRange) => [[startCol, numCols, width]]
columnWidths,
// โญ subranges in dataRange
ranges = {
border, // ็ฒๆก็ท๏ผ(dataRange) => [Range]
gridlines, // ็ดฐๆ ผ็ท๏ผ(dataRange) => [Range]
// โญ ๅฒๅญๆ ผๆ ผๅผ๏ผ(dataRange) => [{format: '0.0', ranges: [Range]}]
numberFormat,
alignCenter, // ็ฝฎไธญ ๏ผ(dataRange) => [Range]
gray, // ็ฐ่ฒ๏ผ(dataRange) => [Range]
lightBlue, // ๆทก่๏ผ(dataRange) => [Range]
lightGreen, // ๆทก็ถ ๏ผ(dataRange) => [Range]
bold, // ็ฒ้ซ๏ผ(dataRange) => [Range]
mergeAcross, // ๆฐดๅนณๅไฝต๏ผ(dataRange) => [Range]
// conditional formats
scores, // ๅๆธๅ๏ผไธๅๆ ผ่
ๆทก็ด
(dataRange) => [Range]
top3, // ็ญๆๅไธ๏ผๆทก่ (dataRange) => [Range]
top30, // ๆ กๆๅไธๅ๏ผๆทก็ถ (dataRange) => [Range]
},
}={ranges: {}}){
// ----------------------- set values into sheet -----------------------
sheet.clear();
sheet.setValues(data);
// ----------------------- format tables -----------------------
// โโโโโ
// โ ็ซ็ท โ
// โโโโโ
// grid lines first
if (ranges.gridlines) {
ranges.gridlines(dataRange).forEach(range =>
range.setBorder(true, true, true, true, true, true)
)
}
// border last
if (ranges.border) {
ranges.border(dataRange).forEach(range =>
range
.setBorder(true, true, true, true, null, null, 'black', SpreadsheetApp.BorderStyle.SOLID_MEDIUM)
)
}
// โโโโโโโ
// โ ๆธๅญๆ ผๅผ โ
// โโโโโโโ
if (ranges.numberFormat) {
ranges.numberFormat(dataRange).forEach(({format, ranges}) =>
ranges.forEach(range => range.setNumberFormat(format))
)
}
// โโโโโ
// โ ๅฐ้ฝ โ
// โโโโโ
if (ranges.alignCenter) {
ranges.alignCenter(dataRange).forEach(range =>
range.alignCenter()
)
}
// โโโโโโโ
// โ ๆฐดๅนณๅไฝต โ
// โโโโโโโ
if (ranges.mergeAcross) {
ranges.mergeAcross(dataRange).forEach(rng => rng.mergeAcross())
}
// โโโโโ
// โ ็ฒ้ซ โ
// โโโโโ
if (ranges.bold) {
ranges.bold(dataRange).forEach(rng => rng.setFontWeight('bold'))
}
// dataRange.nthRow(1) // ๆจ้ก
// .setFontSize(14)
// โโโโโโ
// โ ่ๆฏ่ฒ โ
// โโโโโโ
if (ranges.gray) {
ranges.gray(dataRange).forEach(range =>
range.setBackground(app.color.gray)
)
}
if (ranges.lightBlue) {
ranges.lightBlue(dataRange).forEach(range =>
range.setBackground(app.color.lightBlue)
)
}
if (ranges.lightGreen) {
ranges.lightGreen(dataRange).forEach(range =>
range.setBackground(app.color.lightGreen)
)
}
// โโโโโโโ
// โ ๆฌไฝๅฏฌๅบฆ โ
// โโโโโโโ
if (columnWidths) {
columnWidths(dataRange).forEach(([start, cols, width]) =>
sheet.setColumnWidths(start, cols, width)
)
}
// โโโโโโโ
// โ ๆขไปถๆ ผๅผ โ
// โโโโโโโ
if (ranges.scores) {
sheet.appendConditionalFormatRule(app.rules.redIfLessThan60(ranges.scores(dataRange)))
}
if (ranges.top3) {
sheet.appendConditionalFormatRule(app.rules.blueIfTop3(ranges.top3(dataRange)))
}
if (ranges.top30) {
sheet.appendConditionalFormatRule(app.rules.greenIfTop30(ranges.top30(dataRange)))
}
};
๐พ google apps script
app.makeTable(classNo, Student.dataForClassTable(classNo), {
// column widths
columnWidths: (r, {cols=r.cols()}={}) => [[1, cols, 60]],
// ranges
ranges: {
// border
border: (r) => [r.inset({top: 1, bottom: 1})], // app.range.prototype
// grid lines
gridlines: (r) => [r.inset({top: 1})],
// gray ranges
gray: (r, {rows=r.rows()}={}) => [
r.nthRow(2),
r.cell(rows, 3) // <-- #TODO: r.bottomLeftCell(rowOffset, colOffset)
],
// bold
bold: (r) => [r.nthRow(1)],
// merge across
mergeAcross: (r) => [r.nthRow(1)],
// align center
alignCenter: (r) => [r],
// ----- number format -----
numberFormat: (r, {cols=r.cols()}={}) => [
{format: '0.00', ranges: [
r.inset({top: 2, left: 3, right: 2}),
]},
{format: '00', ranges: [
r.nthColumn(2).inset({top: 2, bottom: 1})
]},
{format: '0', ranges: [
r.lastNColumns(2).inset({top: 2, bottom: 1})
]},
],
// ----- conditinoal format -----
scores: (r, {cols=r.cols()}={}) => [
r.inset({top: 2, left: 3, right: 4}),
r.nthColumn(cols - 2).inset({top: 2, bottom: 1}),
],
top3: (r) => [r.lastNthColumn(2).inset({top: 2, bottom: 1})],
top30: (r) => [r.lastNthColumn(1).inset({top: 2, bottom: 1})],
},
});
nested destructuring โญ๏ธ
Last updated