【问题标题】:Google Aps Scripts - Set a value if input not in rowGoogle Apps 脚本 - 如果输入不在行中,则设置一个值
【发布时间】:2022-01-10 03:08:26
【问题描述】:

如果该值不在一行中,我正在尝试设置一个输入值。我可以获取行的值并设置值。

当我询问输入是否在行中时,出现了我的问题。这是我的脚本:

//Auxiliar Funtion
    function is_in_array(array,value) {
          is_in = false
          Logger.log('This is the value: ' + value)
          for (i = 0; i < array.length; i++) {
            Logger.log('This is i: ' + array[i])
            if (value == array[i]) {
                is_in=true;
                Logger.log('I was here!')
            }
          }
          return is_in
        }
        
//Main
function rm(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getActiveSheet();

  var fecha = ws.getRange(5,9).getValue();

  //Reviso si existe la fecha
  ws = ss.getSheetByName(tipo);
  numCols = ws.getLastColumn();
  fechas = ws.getRange(1,4,1,numCols).getValues()[0];
  Logger.log('Fecha :' + fecha);
  Logger.log('Fechas: ' + fechas);
  is_in_result = is_in_array(fechas, fecha)
  Logger.log('Functions result: ' + is_in_result)

  //Logger.log(ArrayUtils.contains( fechas, fecha ))
  if (is_in_result==true) {
    Logger.log('If statement')
    return True 
  } else {
    Logger.log('Else Statement')
    //Se añade la fecha
    ws.getRange(1,numCols+1).setValue(fecha)
  } 
}

这是日志:

Registros de Cloud
9 ene 2022, 23:51:54    Información Fecha :Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información Fechas: Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental),Fri Oct 15 2021 03:00:00 GMT-0400 (hora de verano oriental),Wed Dec 08 2021 03:00:00 GMT-0500 (hora estándar oriental),,,
9 ene 2022, 23:51:54    Información This is the value: Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información This is i: Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información This is i: Fri Oct 15 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información This is i: Wed Dec 08 2021 03:00:00 GMT-0500 (hora estándar oriental)
9 ene 2022, 23:51:54    Información This is i: 
9 ene 2022, 23:51:54    Información This is i: 
9 ene 2022, 23:51:54    Información This is i: 
9 ene 2022, 23:51:54    Información Functions result: false
9 ene 2022, 23:51:54    Información Else Statement

Fecha 在 Fechas 中,所以函数结果必须为 true,但返回 false :(

我的错误在哪里? 或者,有更简单的方法来获得相同的结果? 也许它不起作用,因为我正在比较两个日期,我不知道......

有些东西是西班牙语,抱歉

【问题讨论】:

    标签: javascript datetime google-apps-script date-comparison


    【解决方案1】:

    您无法将不同的Date 对象与===== 进行比较。比较它们自 Unix 纪元以来的毫秒数与 Date.getTime()

    var date1 = new Date( '2022-01-01' );
    var date2 = new Date( '2022-01-01' );
    
    console.log( 'date1 == date2: ' + ( date1 == date2 ) ); // false
    console.log( 'date1 === date2: ' + ( date1 === date2 ) ); // false
    console.log( 'date1.getTime() == date2.getTime(): ' + ( date1.getTime() == date2.getTime() ) ); // true

    【讨论】:

    • 无论您使用哪种编程语言,日期比较总是一个问题啊哈哈哈。谢谢你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多