0

所以我正在尝试使用谷歌表格和一个自定义函数来制作一个自动轮换时间表,该函数每周从链接的表格中选择一个新员工,然后当它到达底部时再次从列表顶部开始,使用谷歌触发器每 7 天运行一次计数器。

我很难弄清楚如何每周在电子表格中存储一个值以存储计数器的值,然后在函数再次运行以更新计数器时引用相同的值。

我也有一个问题,我的电子表格在我当前的输出中抛出“结果不是数字”错误,可能是因为它指的是它自己,当它只能存储公式时我不知道如何初始化计数器在它所指的单元格中。

这是我所拥有的:

/* counter starts at 2, increases each week (called upon by Google trigger to run each week) until 
it reaches the lastRow of employees and then resets to two.
Returns this week's counter value each time to cell where function is called. */

function cleanerCounter(){ 
  /*sheets*/
  var sheet = SpreadsheetApp.getActive().getSheetByName('KitchenDuties');
  var eDirectory = SpreadsheetApp.getActive().getSheetByName('EmployeeDirectory');
  var lastRow = eDirectory.getLastRow(); //last row that contains an employee in the directory

   //counter setup
  var counter = sheet.getRange(6,2);
  counter = counter.getDisplayValue(); 
  counter = +counter; 

  if(counter >= lastRow){
    counter = 2;
    return +counter;
  } else {
    return +counter;
  }
}
4

2 回答 2

0

一个简单的属性服务计数器

function getCounter() {
  const ps=PropertiesService.getScriptProperties();
  var n=ps.getProperty('counter');
  if(!n){ 
    ps.setProperty('counter', 2);//initialize counter if not there
    var n=2;
    return n;
  }
  if(n>=SpreadsheetApp.openById("Insert your spreadsheet id").getSheetByName('EmployeeDirectory').getLastRow()) {
    ps.setProperty('counter',2)
  }else{
    ps.setProperty('counter',Number(n) + 1);
  }
  return ps.getProperty('counter');
}
于 2020-06-16T23:33:46.780 回答
0

我错了,PropertiesServices 绝对是要走的路,谢谢你的提示。在你有机会回应之前,我做了一些不同的事情,它对我的​​目的来说效果更好:

function cleanerCounter(){ 
  /*sheets*/
  var sheet = SpreadsheetApp.getActive().getSheetByName('KitchenDuties');
  var eDirectory = SpreadsheetApp.getActive().getSheetByName('EmployeeDirectory');
  var lastRow = eDirectory.getLastRow(); //last row that contains an employee in the directory
  var documentProperties = PropertiesService.getDocumentProperties();

  var counter = documentProperties.getProperty('COUNTER');

  counter = parseInt(counter);
  counter++;
  counter = counter.toString();
  documentProperties.setProperty('COUNTER', counter);
  Logger.log('COUNTER =', documentProperties.getProperty('COUNTER'));

  var output = sheet.getRange(2, 6).setValue(parseInt(documentProperties.getProperty('COUNTER')));


}


function resetCounter(){
  var documentProperties = PropertiesService.getDocumentProperties();
  var counter = documentProperties.setProperty('COUNTER', '2');
}
于 2020-06-17T20:03:30.560 回答