【问题标题】:How to make to update which cell is highlighted every day at midnight如何更新每天午夜突出显示的单元格
【发布时间】: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


【解决方案1】:

代码存在一些问题。

第一:

每次运行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)
}

【讨论】:

    【解决方案2】:

    试试这个:

    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);
    }
    

    未测试。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 如何使用条件格式来做到这一点?我先试过了,但我想不通。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 2020-07-26
      相关资源
      最近更新 更多