繁体   English   中英

Google App Script - 根据星期几写入单元格的脚本

[英]Google App Script - Script to write in cells according to the day of the week

我正在尝试将此部分添加到我的 Google 应用程序脚本中。 我想添加一个脚本,根据星期几更改要写入的单元格。 例如:

If day=monday then write in cell S1
If day=tuesday then write in cell T1
If day=wednsday then write in cell U1

ECC。

你知道我怎样才能达到这个结果吗? 谢谢!

编辑:这对我有用!

    if (total != 'error') {
    if(new Date().getDay() === 0){
       sh3.getRange('S'+counter).setValue(total);
    } else if(new Date().getDay() === 1){
       sh3.getRange('T'+counter).setValue(total);
    } else if(new Date().getDay() === 2){
       sh3.getRange('U'+counter).setValue(total);
    } else if(new Date().getDay() === 3){
       sh3.getRange('V'+counter).setValue(total);
    } else if(new Date().getDay() === 4){
       sh3.getRange('W'+counter).setValue(total);
    } else if(new Date().getDay() === 5){
       sh3.getRange('X'+counter).setValue(total);
    } else if(new Date().getDay() === 6){
       sh3.getRange('Y'+counter).setValue(total);
    }
  }

在 Javascript 中,您可以使用new Date().getDay()方法获取星期几(数字索引)。 下一部分是使用sheet.getRange()方法获取对电子表格中目标单元格的引用。

我会通过以下资源向 go 推荐,以了解该解决方案的工作原理:

  1. MDN 上的 getDay()
  2. 获取范围()
  3. 设定值()

解决方案:

以下代码将

  • 在星期天,将字符串“test”写入单元格 S1,
  • 星期一,将字符串“test”写入单元格 S2 等。
const spreadsheetId = "...";
const sheetName = "Sheet1";
const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
const sheet = spreadsheet.getSheetByName(sheetName);
const today = new Date().getDay();
const cell = sheet.getRange(`S${today+1}`)
cell.setValue("test");

如果想跳过周日,周一写入S1,需要修改

if (today > 0) {
  const cell = sheet.getRange(`S${today}`)
  cell.setValue("test");
}

像这样使用 Date() object 的 getDay() 方法:

sheet.getRange(1, new Date().getDay() + 19).setValue()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM