โญ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)))
  }

};

Last updated