Call showSidebar() from a cell in google spreadsheets

假装没事ソ 提交于 2020-04-16 03:43:09

问题


As user Serge insas answered here,

Ui Dialogs can not be called by time triggered functions, they have to be triggered by a user action, that's to say a click on a menu item or some sort of button that calls the function showing the UI.

What about calling showSidebar() or showModalDialog() from a cell? Something like the following formula, which should open the sidebar automatically when the value in A1 is greater than 0:

=if(A1>0;showSidebar();"nothing to show")

Is this possible?

This is my script:

function onOpen() {
  SpreadsheetApp.getUi().createAddonMenu().addItem('Open Beeper', 'openBig').addToUi();
}

function openBig() {
  var htmlContent = HtmlService.createHtmlOutputFromFile('audioHtml').setTitle('Beeper');
  SpreadsheetApp.getUi().showSidebar(htmlContent);
}

I can open the sidebar by clicking the item in the addons menu without problems but when I call openBig() from a cell I get the following error:

Cannot call SpreadsheetApp.getUi() from this context.

What am I doing wrong? What is missing?


回答1:


TheMaster is right. You are doing nothing wrong. Simply, you cannot call SpreadsheetApp.getUi() from the sheet itself (any cell). It is only allowed to be called from the context of the menu.



来源:https://stackoverflow.com/questions/59933874/call-showsidebar-from-a-cell-in-google-spreadsheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!