onOpen event does not not launch a dialog box - google-apps-script

I have a sheet that I send out to staff (copies that are shared) but when I copy this the function onOpen does not launch the dialog box.
function onOpen() {
SpreadsheetApp.getUi()
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Math Sheet Directions');
}

Take a deep look on the next thread : onOpen not executing?
As you can see from the thread and Issue Tracker:
Google has restriction for onOpen functionality
Is there a different way to achieve your goal?
You can create a red button "Show URLs" and assign it a function.
For example:
function openDialogBox() {
// your html content here
var html = "<a href='https://google.com'>Google.com</a><br /><a href='https://stackoverflow.com'>Stackoverflow.com</a>" ;
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(250)
.setHeight(300);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'URL links');
}
This is how it looks like:
We don't have any restriction here.
I know this method doesn't help you, but probably this is the only way to achieve similar behavior of what you want to do :)
Reference
Google Apps Script - onOpen()
Google Apps Script - Class HtmlService
Google Apps Script Guide - HTML Service: Create and Serve HTML

Related

Google Docs Apps Script Loading Indicator

I'm running a script when I open/refresh a Google Doc. It takes some time to run and can affect values in the doc, so I want to show some type of loading indicator to the user until it's done. I would prefer to not overlay a modal, so the doc is still accessible, but apart from that any solution, even a hacky one, would be ok.
// EXECUTE SCRIPT ON OPEN
function onOpen() {
// SHOW LOADING TO USER
initializeDoc();
doSomeStuffThatTakesSomeTime();
const ui = DocumentApp.getUi();
ui.createMenu('MyMenu')
.addItem('Click', 'doSomeStuffThatTakesSomeTime')
.addToUi();
// TURN LOADING OFF
}
Loading...
function loading() {
const ui = DocumentApp.getUi();
ui.showModelessDialog(HtmlService.createHtmlOutput("Loading...."),"Please Wait");
Utilities.sleep(5000);
const hl = '<!DOCTYPE html><html><head><base target="_top"></head><script>window.onload=()=>{google.script.host.close();}</script><body></body></html>';
ui.showModelessDialog(HtmlService.createHtmlOutput(hl),"Good Bye")
}
You will require an installable onOpen() such as below:
function onMyOpen(e) {
loading();
}

Is there a way to put chart created in google apps script in a tab on google sheet?

I am new to google apps scripts and coding but I created a Sankey diagram in google apps script using the google visualization HTML code. I am using the data from a google sheet because it is dynamic data. Currently I successfully deployed the diagram as a web app that updates when refreshed. However, I am wondering if it's possible to put the sheet IN the google sheet - maybe as a separate tab? I want to be able to share the google sheet and have the data and Sankey diagram immediately available.
Here is my code for grabbing the spreadsheet data and publishing it as a web app:
function doGet(e) {
return HtmlService
.createTemplateFromFile("Index")
.evaluate()
.setTitle("Google Spreadsheet Chart")
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getSpreadsheetData() {
var ssID = "XXXXXXXXXXXXXXX",
sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
data = sheet.getDataRange().getValues();
return data;
}
This is for many people to view so I had an alert pop up onOpen that says "Look at the diagram menu" and then a menu pop up onOpen to click on the diagram.
function onOpen() {
alert();
menu();
getSpreadsheetData();
openDiagram();
}
function alert() {
var ui = SpreadsheetApp.getUi();
ui.alert('To see diagrams for different brands, click on the "Diagram" menu.');
}
function menu() {
SpreadsheetApp.getUi()
.createMenu('Diagram')
.addItem('Diagram', 'openDiagram')
.addToUi();
}
function getSpreadsheetData() {
var
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data'),
data = sheet.getDataRange().getValues();
return data;
}
function openDiagram() {
var html = HtmlService.createHtmlOutputFromFile('Index')
.setWidth(1000)
.setHeight(800);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Diagram');
}
Then when I click on the diagram under the menu a window opens with my diagram.

custom menu on google spreadsheet

Need expert help, can we run function directly from the menu, instead of Sub-menu, it fails double clicking.
and I would want to close the dialog box after the download start, and can we delete the sheet after we download it ? :
SpreadsheetApp.getActive().deleteSheet(.getSheetId(sID));
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Download')
.addItem('Download', 'Download')
.addToUi();
}
function Download() {
var ssID = SpreadsheetApp.getActive().getId();
var sID = SpreadsheetApp.getActive().getSheetId();
var URL = 'https://docs.google.com/spreadsheets/d/'+ssID+'/export?format=pdf&gid='+sID;
var htmlOutput = HtmlService
.createHtmlOutput('Click to download')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(160)
.setHeight(60);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Download');
}
If you mean the very top level menu, then the answer would be no.
You can delete a sheet but you will have to put a spreadsheet into the trash first. Although I think it's possible for GSuite Admins to get file delete authorization. I don't know I never bothered with it.
You can close the dialog box with google.script.host.close() Reference
Click Me

Show dialog Google-Apps-Script for a shared link

The code below works on a document I am the owner of. When I share a link to it (with edit privileges), it does not respond for the editor. Any idea what is the cause of it?
function onEdit(event) {
showDialog();
} // I also have this function listed under triggers for 'On edit' events
function showDialog() {
var ui = HtmlService.createHtmlOutputFromFile('revRequestUI')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(700)
.setHeight(320);
SpreadsheetApp.getUi()
.showModalDialog(ui, ' ');
}

Cannot find custom menu and sidebar in the published add-on

I am trying to publish a Google Sheet add-on with the unlisted option: Only people with the link can see it. Here is the code: Code.gs:
function myFun() {
return "myFunValue"
}
function onInstall() {
onOpen()
}
function onOpen() {
var html = HtmlService.createHtmlOutputFromFile('Index.html') //your html page name
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Use in this spreadsheet', 'use')
.addToUi();
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
function use() {
var title = 'Date Custom Functions';
var message = 'Message of use';
var ui = SpreadsheetApp.getUi();
ui.alert(title, message, ui.ButtonSet.OK);
}
Index.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
super add-on, with custom function "=myFun()" and custom menu
</body>
</html>
Here is the link:
https://chrome.google.com/webstore/detail/functionmenuside/bmojfiljhggogegocdkjdlaoohbkcmod?authuser=0
This add-on works on its own Google Sheet. However, after publishing, when I click on +Free, it opens a new Google Sheet, I can find FunctionMenuSide in the Add-ons. The custom function works, whereas there is no sidebar, and we cannot find the custom menu Use in this spreadsheet. It seems that onOpen is not executed.
Does anyone know how to fix this?
Edit 1:, in the console, there are messages:
1739997376-ritz_waffle_integrated_i18n_ritzmaestro.js:105 Google Apps Script: You do not have permission to call createHtmlOutputFromFile.
You are trying to open the sidebar in the onOpen() function. This won't work because of the way that simple triggers work with Google Apps Script.
Instead, create another function that opens the sidebar and adds that function as an item in your add-on menu. Then your user can open the sidebar themselves from the add-on menu.