What's the difference between ui.alert and Browser.msgBox? - google-apps-script

When working with Google Scripting, there's a Browser.msgBox(); (Link) and ui.alert(); (Link). What is the difference between the two? They appear to do the exact same thing.
There are more methods within, such as Browser.inputBox(); and ui.prompt(); which again, appear to be identical.

The Browser Class is only available to a Spreadsheet. The Ui Class can be more widely used. Unfortunately, the documentation for Class Ui only shows an example of the getUi() method with the SpreadsheetApp Class. But getUi() is available to DocumentApp.
DocumentApp.getUi()
And to:
FormApp.getUi()
If you try to call Browser.msgBox() from the wrong context, you'll get an error:
Cannot call Browser.msgBox() from this context; have you tried Logger.log() instead?
Browser.msgBox() is easier to use in a Spreadsheet script. You don't need to first use var ui = SpreadsheetApp.getUi();
To compare:
Browser.msgBox('prompt here');
SpreadsheetApp.getUi().prompt('prompt here');

Related

GAS TypeError: SpreadsheetApp.getUI is not a functionDetails

I'm trying to learn some basic google scripting.
I tried reading through this several times, thought about it overnight and searched all over SO and a google for answers and feel like I'm missing something stupid.
When I click the menu item I've added to my gsheet to kick of the sidepage I get this error:
TypeError: SpreadsheetApp.getUI is not a function Details(/link)
If I click that details link I get the same message in a message box:
TypeError: SpreadsheetApp.getUI is not a function
Here's my code:
//#OnlyCurrentDoc
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu("Admin")
.addItem("Admin page", "showAdminSidebar")
.addToUi();
}
function showAdminSidebar(){
var widget = HtmlService.createHtmlOutput("<h1>Sidebar<h1>");
SpreadsheetApp.getUI().showSidebar(widget);
}
I know I must be missing something simple, so thanks for any advice you can give me.
As pointed by #TheMaster above, there is a typo on the method showAdminSidebar().
...SpreadsheetApp.getUI().showSidebar(widget);... where getUI() should be getUi().
See this documentation of methods the class SpreadsheetApp has.
Just tested here and it works just fine after fixing it.

Google Apps Script SpreadsheetApp Banding Class - How to access these methods

I am trying to figure out how to access the Banding Class of methods for the SpreadsheetApp. This is not how to simply apply banding. Instead, I am trying to get the data related to a sheet that banding has been applied to.
Here is the Developers Documentation:
https://developers.google.com/apps-script/reference/spreadsheet/banding
I can use .getBandings() but this simply confirms whether or not a banding exits in the sheet. There is no data as to the details of that banding. For example, I would like to return the range of the banding.
I also learned that when a banding is applied, the background color of the cell stays default, so I cannot utilize those methods to determine the details.
Here is the link to a Sheet with banding applied. I have added a few lines of script to show what getBandings() returns. Feel free to make a copy.
https://docs.google.com/spreadsheets/d/1xRuwE8moueSY5ZizZcy6KPkVmhJmOITN9Bql7XgHY3g/edit#gid=0
Any advice on how to access/utilize the methods in the Banding class would be appreciated.
From a comment to an answer
I am trying figure out the list of methods that would open up that class in SpreadsheetApp. I have experimented with SpreadsheetApp, getActiveSheet, getSheetByName, getRange and more. I cannot figure out how to open up that set of commands in the Banding class.
For example, I would like to return the range of the banding.
To get the range of a banding use the getRange() method of Class Banding
getBandings() returns an interable object. Below is a simple example of how to get the address of the range of a banding. It assumes that the spreadsheet has at least one banding.
function myFunction(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var bandings = spreasheet.getBandings();
var range = bandings[0].getRange();
Logger.log(range.getA1Notation());
}
Related
Apply a "row banding" theme to a range

Button doesn't work on google spreadsheet

I am working to link an image in my Google Sheet document to a specific cell in another tab. I'm doing this by building a simple function that will do this. However, when I assign the function and then click on the image, I then get the error :
Script function "test" could not be found
When I run the function in the script manager interface, it works fine. It's when I try to actually use it in the sheet with the image.
Function Script :
function test()
{
Browser.msgBox("You clicked it!");
}
It turned out that the document owner had left their job and ownership rights had been moved to someone else. Can it matter ?
The error is : Here
Thank you very much,
Make sure the assigned function name does not include the () brackets. 😎
Did a little snippet just to demonstrate on how you might approach this:
I used the sample from HTML Service: Create and Serve HTML on creating a button (in your case it's image) which responds to a click event. I'm using a bound script.
//in bounds script, this integral function triggers as soon as you open the spreadsheet
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Dialog')
.addItem('Click Me', 'test')
.addToUi();
}
//Then I attached your test function
function test()
{
Browser.msgBox("You clicked it!");
}
And sure enough upon clicking the button, the test function triggered:

Autocomplete in google apps script editor not always aware of type/context

I'm getting started with Google Apps scripting, and find the autocomplete very useful. However, once you are inside a new function, autocomplete doesn't seem to have any way of knowing what the type is for the parameter. I've seen some answers about python ideas that say that using javadoc will work. But I'm not able to figure it out. Any suggestions?
function myfunc1(){
var activeSheet=SpreadsheetApp.getActiveSheet();
activeSheet//.autocomplete works here
myfunc2(activeSheet)
}
function myfunc2(myActiveSheet){
myActiveSheet//.autocomplete doesn't work here
}
There are limitations to what the UI can do in terms of autocomplete.
Usually I just keep the reference documentation open in another tab and refer to that, but you can also trick the UI into auto completing using comments:
function myfunc2(myActiveSheet){
/*
var myActiveSheet = SpreadsheetApp.getActiveSheet()
*/
myActiveSheet //.autocomplete now works here
}
The new editor uses JSDoc for parameter types. So declare the parameter in the docs, and specify its type between the braces {}.
/**
* #param {SpreadsheetApp.Sheet} sheet
*/
function myfunc(sheet) {
sheet //.autocomplete now works here
}

Set height of Google Apps Script showModalDialog when using an HtmlService HtmlTemplate

I'm currently in the process of changing over my Google Apps Scripts that use the deprecated UI service to the HtmlService.
I've created a modal dialogue using the following code (in a spreadsheet container-bound script):
var htmlTemplate = HtmlService.createTemplateFromFile('testDialogue');
htmlTemplate = template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(htmlTemplate, 'Test Dialogue');
The dialogue box opens, but I need to modify its dimensions.
HtmlOutput objects have a setHeight method, but there doesn't seem to be the same method available for HtmlTemplate objects.
I tried using the method anyway on the object like this:
var htmlTemplate = HtmlService.createTemplateFromFile('testDialogue').setHeight(300);
But that produces this error:
TypeError: Cannot find function setHeight in object HtmlTemplate
Also, I checked the SpreadsheetApp Ui Class and showModalDialog method but neither of them seem to have methods for setting the height of HtmlTemplate objects.
The .setHeight() method can be used when chaining it after the .evaulate() method, like so:
template = template.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setHeight(300);
Update 2/19/19: The .setSandboxMode() method no longer has any effect - now all scripts now use IFRAME mode regardless of what sandbox mode is set (documentation). That method was not related to setting the height but I figured I'd mention this in case anyone ends up copying and pasting this code sample.