LoginSignup
0
0

More than 1 year has passed since last update.

GASでSpreadsheetの範囲を列番号で指定するのが怖い時用のクラスを作った

Last updated at Posted at 2021-12-05

このクラスをGoogle scriptのファイルに追加してあげると、、、

IndicatableRow.gs
class IndicatableRow {
  constructor(firstRow, row){
    this.row = row
    this.firstRow = firstRow
  }
  get(key){
    var index = this.firstRow.indexOf(key)
    if ( typeof index != "number" && index ) { throw `No such key: ${key}` }
    return this.row[index]
  }
  getIndex(key){
    return this.firstRow.indexOf(key)
  }
  toObject(){
    var obj = {}
    this.firstRow.forEach((columnName)=>{
      obj[columnName] = this.get(columnName)
    })
    return obj
  }
}

以下のような形で「1行目の名前」で列のデータを指定できるようになるので、
列の追加や並び替えに強いScriptを作れます。

main.gs
function main() {
  const targeData= targetSettingsSheet.getDataRange.getValues()
  const propertyName = settingsSheet.getRange("B4")
  const firstRow = targeData.shift()
  targetData.forEach(function(row){
    const indicatableRow = new IndicatableRow(firstRow, row)
    const pageId = getNotionPageId(indicatableRow.get("Relationを追加するページのURL"))
    const relationPageId = getNotionPageId(indicatableRow.get("追加されるRelationのURL"))
    addNotionRelation(pageId, relationPageId, propertyName)
  })
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0