使用谷歌应用程序脚本的确认消息框

Confirmation Message Box Using Google Apps Script

本文关键字:确认 消息 脚本 谷歌 应用程序      更新时间:2023-09-26

我已经实现了谷歌应用程序脚本代码来显示确认消息,我成功地使用标签显示了消息,但我试图在一个框中而不是标签中显示确认消息。这样用户就可以清楚地看到他们成功提交了文件,并可以点击该框中的"确定"按钮。我尝试使用下面的代码来实现这个目标,但它显示了以下错误消息:

"遇到错误:SpreadsheetApp.getUi()只能从绑定到新的Google Sheets的版本。"

这是我为编写谷歌应用程序脚本而创建的新谷歌电子表格,但我复制并粘贴了现有的代码。

这是我试图用来显示确认消息框的代码(显示上面列出的错误消息):

var ui = SpreadsheetApp.getUi();
   var userChoice = ui.alert(
      'Thank You for uploading files',
      ui.ButtonSet.OK);
   if (userChoice == ui.Button.OK) {
      ui.alert('Operation Confirmed.');
   } else {
      ui.alert('Operation Halted.');
   }

以下是我成功在标签中显示确认消息的代码:

// Create form to hold the file upload buttons for choosing a file and uploading
var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
  var formContent = app.createVerticalPanel();
  form.add(formContent);
  formContent.add(app.createLabel('All of the following files must be submitted prior to completing the form').setHeight('25px').setStyleAttributes(_lbl).setStyleAttribute('font-size','20px'));

  formContent.add(app.createLabel('Reference Letter').setHeight('20px').setStyleAttributes(_lbl));
  formContent.add(app.createFileUpload().setName('Reference'));
  formContent.add(app.createLabel('Reference Letter 2').setHeight('20px').setStyleAttributes(_lbl));
  formContent.add(app.createFileUpload().setName('Reference2'));
  formContent.add(app.createLabel('Reference Letter 3').setHeight('20px').setStyleAttributes(_lbl));
  formContent.add(app.createFileUpload().setName('Reference3'));
formContent.add(app.createSubmitButton('Submit File Uploads'));
function doPost(e) {
  // data returned is a blob for FileUpload widget
  var fileBlob = e.parameter.Reference;
  var doc = DocsList.createFile(fileBlob);
  var app = UiApp.getActiveApplication();
  var fileBlob2 = e.parameter.Reference2;
  var doc = DocsList.createFile(fileBlob2);
  var app = UiApp.getActiveApplication();
  var fileBlob3 = e.parameter.Reference3;
  var doc = DocsList.createFile(fileBlob3);
  var app = UiApp.getActiveApplication();
  //Display a confirmation message
  var label = app.createLabel('Thank You for uploading files').setStyleAttribute('font-weight','bold').setStyleAttribute('font-size','1.1em');
  app.add(label);
  return app;
}

有没有办法在方框中而不是在标签中显示确认消息?我还必须显示程序其他部分的确认消息。

感谢

您可以使用DialogBox或PopupPanel来代替标签。有关详细信息,请查看链接的文档。

顺便说一句,doPost()函数代码将var app设置了三次,这是不必要的。你可能还想编辑你的代码,以说明不是所有3个文件都被选择和上传。