如果带有时间的语句等于0:00 Google Apps脚本

时间:2018-11-20 17:35:28

标签: javascript datetime google-apps-script google-sheets

我在google apps脚本中存在一个单元格问题,如果单元格=“ 0:00”,则该单元格必须等于“ Unexcused Absence”之类的文本。当我运行代码时,它给了我一个空白单元格。

function Boh() {

  var sheet = SpreadsheetApp;

  var ss = sheet.getActiveSpreadsheet().getActiveSheet();

  var lastRow = ss.getLastRow();

  var spreadsheet = SpreadsheetApp.getActive();

  var b = 0;

  var wCell = 'Work';

  var x = new Date('3/16/2013 0:00:00');


  for(var i=2;i<=lastRow;i++){ 
  ss.getRange(i,10).activate();

  if(x.setHours(0,0,0,0)  == ss.getRange(i, 17).getValue() && 'Work' == ss.getRange(i, 5).getValues()){

     ss.getRange(i, 10).setValue('Unexcused Absence');
   } 

 }

1 个答案:

答案 0 :(得分:0)

我不使用录制的宏,因此不确定其工作原理。这将在主菜单中添加一个菜单项,您可以从那里运行脚本。试试这个。

function onOpen() {
  var thisMenu = SpreadsheetApp.getUi().createMenu('Boh');
  thisMenu.addItem('Boh', 'Boh').addToUi();
}

function Boh() {
  try {
    var spread = SpreadsheetApp.getActiveSpreadsheet();
    // Be sure you are on the right sheet when you run this
    var sheet = spread.getActiveSheet();
    // Get everything
    var values = sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).getValues();
    // results will be put in column J later
    var results = [];
    // Skip row 1
    for(var i=1; i<values.length; i++ ) {
      //  Assuming ss.getRange(i,17).getValue() is a duration, column 17 or Q becomes values[i][16]
      if( ( Utilities.formatDate(values[i][16],Session.getScriptTimeZone(),"HH:mm:ss") === "00:00:00" ) &&
          ( values[i][4] === "Work" ) ) {
        results.push(["Unexcused Absence"]);
      }
      else {
        // Notice I push an array to make results a 2D array of n rows 1 column
        // values[i][9] is column 10 or J
        results.push([""]);
      }
    }
    // Note we skipped line 1
    sheet.getRange(2,10,results.length,1).setValues(results);
  }
  catch(err) {
    Logger.log(err);
  }
}