Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script else如果字符串匹配循环google脚本(电子表格)_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script else如果字符串匹配循环google脚本(电子表格)

Google apps script else如果字符串匹配循环google脚本(电子表格),google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在从事一个项目,我设计了一个签出按钮,可以自动填充共享电子表格中的单元格。 我对else-if循环中的逻辑有问题。我希望有一个UI,告诉用户字符串不匹配的时间。到目前为止,我已经能够为我的电子表格创建菜单、提示和匹配函数,但是我仍然停留在else-if语句中,该语句返回not-found消息 if (button == ui.Button.OK) { text = result.getResponseText().toUpperCase(); for(n=0;n<data.len

我正在从事一个项目,我设计了一个签出按钮,可以自动填充共享电子表格中的单元格。 我对else-if循环中的逻辑有问题。我希望有一个UI,告诉用户字符串不匹配的时间。到目前为止,我已经能够为我的电子表格创建菜单、提示和匹配函数,但是我仍然停留在else-if语句中,该语句返回not-found消息

if (button == ui.Button.OK) 
{

text = result.getResponseText().toUpperCase();

for(n=0;n<data.length;++n)
{ // iterate row by row and examine data in column A
  if(data[n][0].toString().match(text)==text)
  {
    data[n][13] = newDate;
    data[n][14] = newTime;
    data[n][15] = 'YES';
    data[n][16] = 'YES';
    //data[n][0] = data[n][0].setBackgroundColor('red');


    var result2 = ui2.prompt(
    'Checkout process in progess',
    'Please enter yout initials: (eg. MR)',
    ui2.ButtonSet.OK);
    var text2 = result2.getResponseText().toUpperCase();
    data[n][17] = text2;

  }  
Logger.log(data)
sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet

}
任何帮助都将被高度赞赏,这是我第一次在谷歌应用程序中编码,到目前为止我真的很喜欢它。
请给我一些想法。

如果您已经展示了整个代码,这会更容易,但我认为您甚至不需要使用else-if语句,简单的else就足够了,因为您对单个值只有一个条件

除此之外,您还需要另一种设置背景颜色的方法:您使用的数组只有电子表格值,它不再是范围对象。。。我建议使用第二个带有背景色的数组,并更新该数组的值

代码如下所示:

  var BgColors = sheet.getDataRange().getBackGroundColors();
  if (button == ui.Button.OK){
    text = result.getResponseText().toUpperCase();
    for(n=0;n<data.length;++n){
      // iterate row by row and examine data in column A
      if(data[n][0].toString().match(text)==text){
        data[n][13] = newDate;
        data[n][14] = newTime;
        data[n][15] = 'YES';
        data[n][16] = 'YES';
        BgColors[n][0] = '#ff0000';// update color
        var result2 = ui2.prompt(
          'Checkout process in progess',
          'Please enter yout initials: (eg. MR)',
          ui2.ButtonSet.OK);
        var text2 = result2.getResponseText().toUpperCase();
        data[n][17] = text2;
      }else{
        ui.alert('room number not found; please fill in manually.'); 
        break;
      }
      Logger.log(data)
      sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
      sh.getRange(1,1,BgColors.length,BgColors[0].length).setBackGroundColors(BgColors); // write back to the sheet
    }
  }

它起作用了,我做了一些更改,最后使用布尔值来提示警报消息,但我感谢您的方法。新的背景阵列是个好主意。非常感谢。
  var BgColors = sheet.getDataRange().getBackGroundColors();
  if (button == ui.Button.OK){
    text = result.getResponseText().toUpperCase();
    for(n=0;n<data.length;++n){
      // iterate row by row and examine data in column A
      if(data[n][0].toString().match(text)==text){
        data[n][13] = newDate;
        data[n][14] = newTime;
        data[n][15] = 'YES';
        data[n][16] = 'YES';
        BgColors[n][0] = '#ff0000';// update color
        var result2 = ui2.prompt(
          'Checkout process in progess',
          'Please enter yout initials: (eg. MR)',
          ui2.ButtonSet.OK);
        var text2 = result2.getResponseText().toUpperCase();
        data[n][17] = text2;
      }else{
        ui.alert('room number not found; please fill in manually.'); 
        break;
      }
      Logger.log(data)
      sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
      sh.getRange(1,1,BgColors.length,BgColors[0].length).setBackGroundColors(BgColors); // write back to the sheet
    }
  }