【发布时间】:2020-06-27 22:52:38
【问题描述】:
我正在尝试制作每天午夜更新的 Google Sheet Apps 脚本。我希望它改变每天突出显示的单元格。我项目中的 9 张纸中的每一张都代表一个虚构的月份,每个月份有 40 天。我希望第 5 个月 (Van) 成为第一个用户,第 12 天应突出显示 (D13)。在通过 Voan 的 40 天循环后,它应该会持续到下个月(从 1 开始)。当它到达最后一个月时,它应该在第一个月(Peylior)重新开始一年。
这里是谷歌表格:
https://docs.google.com/spreadsheets/d/1d6aVBQo3plrW0Glx4LQf987j1PGiwq5BnklhvtyEh4c/edit? usp=分享
这段代码能做到这一点吗:
function addTrigger() {
ScriptApp.newTrigger("updateCell").timeBased().atHour(0).everyDays(1).create();
}
function updateCell() {
//set color
var color = (255,0,0)
//open google sheets
var ss = SpreadsheetApp
//open the spreadsheet
var sheet = ss.getActiveSpreadsheet()
//create mnths array
var mnths = [
"Peylior",
"Memnor",
"Rollur",
"Uduir",
"Voan",
"Azzaz",
"Waap",
"Dustrem",
"Predco",
]
//get "System"
var sys = sheet.getSheetByName("System")
//find the month
var mnthind = mnths.indexOf(sys.getRange(1,2).getValue())
//get day
var day = sys.getRange(2,2).getValue()
//add 1 to day
day += 1
//check if month is over
if (day > 40) {
//reset days
day = 1
//change month to next in array unless a year has passed
if (mnthind=12) {sys.getRange(1,2).setValue(mnths[1])}
sys.getRange(1,2).setValue(mnths[mnthind+1])
}
//set the background of current day to color
sheet.getSheetByName(mnths[mnthind]).getRange(day+1,5).setBackground(color)
//update all
sys.getRange(1,2).setValue(mnths[mnthind])
sys.getRange(2,2).setValue(day)
}
【问题讨论】:
-
问题中的代码有什么问题?
标签: javascript google-apps-script google-sheets triggers gs-conditional-formatting