このクラスを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)
})
}