【问题标题】:How to create an if statement to insert text into a UI to enter into Google Sheets cell?如何创建 if 语句以将文本插入 UI 以输入 Google 表格单元格?
【发布时间】:2022-09-29 04:56:55
【问题描述】:

在下面的代码中,promptUPDATE、promptUPDATE2 和 promptUPDATE3 在我按 OK 后都输出“用户点击了“否”或对话框的关闭按钮。我认为我的 if 语句可能有问题,但我不确定是什么。代码应该只在我关闭框或单击否时输出该消息。

function TextBox() {
  // SET UI
var ui = SpreadsheetApp.getUi();

// SET HOT & WARM WORKSHEET
  const sheet = SpreadsheetApp.openById(\'1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4\').getSheetByName(\'HOT WARM CLIENTS\');
// SORT WORKSHEET BY DATE OF NEXT ACTIVITY
      sheet.sort(9)
var today = Utilities.formatDate(new Date(), \"GMT+1\", \"MM/dd/yy\")
var ui = SpreadsheetApp.getUi();
var valuesToCopy = sheet.getRange(\"D5:J5\").getDisplayValues()



var response = ui.alert(valuesToCopy,\"Did we do work for this client today?\", ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
    const variable6 = sheet.getRange(\"G5\").setValue(today);
} else { 
  Logger.log(\'The user clicked \"No\" or the dialog\\\'s close button.\');
}



function promptUPDATE3(){

  // Prompt for the value
  var UPDATE =  SpreadsheetApp.getUi().prompt(valuesToCopy+\"\\n\\n What did we help this client with today?\").getResponseText();

  // Get the sheet that you want store the value in and set the value in the cell B3
 if (response == ui.Button.OK) {
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName(\"HOT WARM CLIENTS\").getRange(\"PREVIOUS\").setValue( UPDATE );  
} else {
  Logger.log(\'The user clicked \"No\" or the dialog\\\'s close button.\');
}}
{ 
  promptUPDATE3();
}
function promptUPDATE()
{
  // Prompt for the value
  var UPDATE =  SpreadsheetApp.getUi().prompt(valuesToCopy+\"\\n\\n When should we follow up with this client next? (MM/DD/YY)\").getResponseText();

  // Get the sheet that you want store the value in and set the value in the cell B3

if (response == ui.Button.OK) {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName(\"HOT WARM CLIENTS\").getRange(\"I5\").setValue( UPDATE );  
} else {
  Logger.log(\'The user clicked \"No\" or the dialog\\\'s close button.\');
}}

{ 
  promptUPDATE();
}


function promptUPDATE2()
{
  // Prompt for the value
  var UPDATE =  SpreadsheetApp.getUi().prompt(valuesToCopy+\"\\n\\n Next Follow Up Activity?\").getResponseText();

  // Get the sheet that you want store the value in and set the value in the cell B3
 if (response == ui.Button.OK) {
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName(\"HOT WARM CLIENTS\").getRange(\"J5\").setValue( UPDATE );  
} else {
  Logger.log(\'The user clicked \"No\" or the dialog\\\'s close button.\');
}}

{ 
  promptUPDATE2();

};};

    标签: google-apps-script google-sheets


    【解决方案1】:

    关于In the below code, promptUPDATE, promptUPDATE2, and promptUPDATE3 all output "The user clicked "No" or the dialog's close button" after I press OK. I think there may be something wrong with my if statements but I'm not sure what.,

    修改点:

    • 在您的脚本中,response 的相同值var response = ui.alert(valuesToCopy,"Did we do work for this client today?", ui.ButtonSet.YES_NO); 用于所有 4 个if (response == ui.Button。并且,在var UPDATE =,,, 处,按钮未设置,response 未更改。
      • 我认为这可能是您的问题的原因。
    • sheetui 可以与同一功能中的另一部分一起使用。
    • 我认为在您将消息和范围填充为数组的情况下,脚本可能会更简单。

    当这些点反映在你的脚本中时,下面的修改怎么样?

    修改后的脚本:

    function TextBox() {
      var ui = SpreadsheetApp.getUi();
      const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT WARM CLIENTS');
      sheet.sort(9)
      var today = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yy")
      var ui = SpreadsheetApp.getUi();
      var valuesToCopy = sheet.getRange("D5:J5").getDisplayValues()
      var response = ui.alert(valuesToCopy, "Did we do work for this client today?", ui.ButtonSet.YES_NO);
      if (response == ui.Button.YES) {
        const variable6 = sheet.getRange("G5").setValue(today);
      } else {
        Logger.log('The user clicked "No" or the dialog\'s close button.');
      }
    
      // I modified the below script.
      // This is from your showing script.
      var ar = [
        { // promptUPDATE3
          message: valuesToCopy + "\n\n What did we help this client with today?",
          range: "PREVIOUS"
        },
        { // promptUPDATE
          message: valuesToCopy + "\n\n When should we follow up with this client next? (MM/DD/YY)",
          range: "I5"
        },
        { // promptUPDATE2
          message: valuesToCopy + "\n\n Next Follow Up Activity?",
          range: "J5"
        }
      ];
      ar.forEach(({ message, range }) => {
        var res = ui.prompt(message, ui.ButtonSet.OK_CANCEL);
        if (res.getSelectedButton() == ui.Button.OK) {
          sheet.getRange(range).setValue(res.getResponseText());
          SpreadsheetApp.getUi(); // If you want to show the updated situation during the script is run, please use this.
        } else {
          Logger.log('The user clicked "No" or the dialog\'s close button.');
        }
      });
    }
    

    参考:

    【讨论】:

    • 谢谢,这很好用。一个问题,“上一个”是一个连续的单元格范围,我只希望更新范围中最左边的单元格,而不是更新范围中的每个单元格。如何才能做到这一点?
    • @Ricardo Barrios 感谢您的回复。我很高兴你的问题得到了解决。关于你One question, "PREVIOUS" is a range of cells in a row and I want only the leftmost cell in the range to be updated instead of every cell in the range. How can this be done?的新问题,我想支持你。但是,我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解你的新问题。我可以问你关于你的新问题的细节吗?
    • 上述代码中的“PREVIOUS”是一个命名的单元格区域 A1:A5。我希望 A1 只有在它为空时才被填充。如果 A1 有内容,我只希望 A2 被填充。如果 A1 和 A2 有上下文,则填写 A3 等
    • @Ricardo Barrios 感谢您的回复。我不得不再次为我糟糕的英语水平道歉。不幸的是,来自“PREVIOUS” in the above code is a named range of cells A1:A5. I want A1 only to be filled if it is empty. If A1 has contents, I want A2 only to be filled. If A1 and A2 have contexts, fill A3, etc。我仍然无法理解你的新问题。但我愿意支持你。因此,在这种情况下,我想建议将此作为一个新问题发布。这样,它将帮助用户思考解决方案。这是因为我的英语水平很差。我再次为此深表歉意。
    • 无论如何,我想出了一个更好的方法,谢谢你的帮助!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多