如何更新每天午夜突出显示的单元格



我正在尝试制作一个每天午夜更新的Google Sheet Apps Script。我希望它每天更改突出显示的单元格。我的项目中的 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)
}

代码存在一些问题。

第一:

每次运行addTrigger()时,都会向项目中添加新触发器,因此请务必检查项目的触发器并删除重复的触发器,您可能不需要。

第二:

您可能还希望取消突出显示前一天,因此您可能需要对其进行编码。

第三:

您的 if 是分配而不是比较。使用双等号 (==( 进行比较。

第四:

您的日历有 9 个月而不是 12 个月,因此已进行调整。

例:

function updateCell() {
var sheet = SpreadsheetApp.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()

//remove the background of past day to color
sheet.getSheetByName(mnths[mnthind]).getRange(day+1,4,1,2).setBackground("#d9d9d9")

//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 == 8) {mnthind = 0}
else{ mnthind++}
sys.getRange(1,2).setValue(mnths[mnthind])
}

//set the background of current day to color
sheet.getSheetByName(mnths[mnthind]).getRange(day+1,4,1,2).setBackground("yellow")
//update all
sys.getRange(1,2).setValue(mnths[mnthind]) 
sys.getRange(2,2).setValue(day)
}

试试这个:

function updateCell() {
const color='#ff0000';
const def='#111111';//default color
const mnths=["Peylior","Memnor","Rollur","Uduir","Voan","Azzaz","Waap","Dustrem","Predco"];
const ss=SpreadsheetApp.getActive();
const sys=ss.getSheetByName("System");
var mnthind=mnths.indexOf(sys.getRange(1,2).getValue());//get current data before incrementing day
var sheet=ss.getSheetByName(mnths[mnthind]);//current month before change
var day=sys.getRange(2,2).getValue();//current day before change
day+=1;
if (day>40) {
day=1;
sheet.getRange(2,4,sheet.getLastRow()-1,1).setBackground(def);
if (mnthind==8) {
mnthindx=0;
sys.getRange(1,2).setValue(mnths[mnthind]);
}else{
mnthind+=1;
sys.getRange(1,2).setValue(mnths[mnthind])
}
}
sheet=ss.getSheetByName(mnths[mnthind]).getRange(2,4,sheet.getLastRow()-1,1).setBackground(def);//set column to default color
ss.getSheetByName(mnths[mnthind]).getRange(day+1,4).setBackground(color);//current day to selected color
sys.getRange(1,2).setValue(mnths[mnthind]); //save current day and month in sys
sys.getRange(2,2).setValue(day);
}

未经测试。

我认为您不希望Google应用程序脚本执行此操作,只需使用条件格式即可

最新更新