【问题标题】:Creating a pop-up form on google sheets using custom forms使用自定义表单在 Google 工作表上创建弹出式表单
【发布时间】:2020-11-20 03:07:44
【问题描述】:

我对在谷歌表格上创建函数非常陌生,因此我在努力创建从弹出表单返回值的代码。 我一直在使用 Google Apps Script 中的这段代码:

var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Getting to know you', 'May I know your name?', ui.ButtonSet.YES_NO);

if (response.getSelectedButton() == ui.Button.YES) {
  Logger.log('The user\'s name is %s.', response.getResponseText());
} else if (response.getSelectedButton() == ui.Button.NO) {
  Logger.log('The user didn\'t want to provide a name.');
} else {
  Logger.log('The user clicked the close button in the dialog\'s title bar.');
}

并且想知道是否有办法将 response.getResponseText 返回到单元格。 当我使用“logger.log”部分所在的“return”函数时,我不断收到错误 - 'Cannot call SpreadsheetApp.getUi() from this context'

在编辑此脚本时是否有另一种方法,或者我应该以不同的方式解释为用户交互获取弹出表单。

谢谢

【问题讨论】:

    标签: google-apps-script google-sheets-custom-function


    【解决方案1】:

    自定义函数有限制,不能调用SpreadsheetApp.getUi()

    作为替代方案,您可以绘制一个自定义按钮,您可以assign a script

    您可以设计脚本,使其将值设置到单击按钮时处于活动状态的单元格中。

    示例

    function myFunction() {
      var cell = SpreadsheetApp.getCurrentCell();
      var ui = SpreadsheetApp.getUi();
      var response = ui.prompt('Getting to know you', 'May I know your name?', ui.ButtonSet.YES_NO);
      var output;
      if (response.getSelectedButton() == ui.Button.YES) {
        output = 'The user\'s name is '+ response.getResponseText();
      } else if (response.getSelectedButton() == ui.Button.NO) {
        output = 'The user didn\'t want to provide a name.';
      } else {
        output = 'The user clicked the close button in the dialog\'s title bar.';
      }
      cell.setValue(output);
    }
    
    

    【讨论】:

    • 我已经用这个更新了代码,它运行良好,谢谢。我目前正在尝试使其适应我的特定功能,因此想知道您是否知道在没有响应框的情况下有一个是/否选项?
    • 可能是一个复选框?
    • 我使用了“.alert”作为应该的“.prompt”,它完成了这项工作。 (但感谢您的快速回复)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2015-05-07
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 2018-12-19
    • 2018-08-28
    相关资源
    最近更新 更多