Section 1 - Google Apps Starter Guide
Section 1 - Google Apps Starter Guide
Workspace you can use that as well. There are some differences on permissions and
who can access the documents between the organization accounts and the regular
Google accounts.
Google Apps Script has many services that can be used, in addition to other Google
Services that can be accessed with the code.
There is a reference guide located at
https://developers.google.com/apps-script/reference
There is a lot that can be done with apps script, including publishing web apps,
creating custom functions in Sheets, adding menus and dialogs to Docs, Sheets and
Forms, interacting with other Google Services like Maps, Drive, Gmail, Calendar and
much much more. You can also build and publish addons to the Google Workspace
Marketplace.
A prerequisite to being able to write App Script code is JavaScript. Apps Script code is
written in modern JavaScript, in addition there are built in libraries that can be accessed
within the Google Workspace products like Sheets, Docs, Gmail, Calendar, Drive and
more. There is nothing to install. You can write and edit the code directly in your
Browser.
Log into your Google Account and in the script dashboard create a new project. This
will open the project in the Google Apps Script Editor. https://script.google.com/
2
By default the apps script editor will create a file called Code.gs and create a empty
function in the editor called function myFunction(){}
Add a line of code within the function Logger.log(‘hello’); and using the Run button
select the function and execute the code.
The Logger is a built-in function in apps script that allows you to send data to be
displayed in the execution log. The execution log will display once the function is run if
the Logger function is called. This log is lightweight and streams in real time, but
4
persists only for a short time and can be used for error checking, debugging and
retrieving information as the code runs.
In the code editor you can write regular JavaScript code, as Apps Script and JavaScript
code runs within a runtime or runtime environment containing the JavaScript engine that
parses and executes script code.
Within your code editor add some JavaScript code, run the function. In order to run the
code it should be contained within a function that can be selected from the drop down
menu.
function testScript1() {
Logger.log('Hello World');
for(let x=0;x<10;x++){
Logger.log(x);
}
}
function testScript2() {
const a = 'string';
const b = 100;
const c = true;
const d = ['one','two',4];
const e = {
first : 'Laurence',
last : 'Svekis'
};
if(5>3){
Logger.log('was true');
}
5
d.push('three');
d.forEach(ele=>{
Logger.log(ele);
})
Logger.log('Hello World');
}
You can manage existing deployments, to either update or create new versions. Also
once you deploy you will be able to see the deployments in the Test deployments tab.
6
Using the test deployment allows you to update your script and see the changes right
away in the dev test deployment URL, which will end with dev. To update and make
changes to your live version which will have a URL ending with exec, you need to
redeploy once the changes are made.
Create your own web app, using the doGet() return back a custom message with HTML
to be output into your web app.
function doGet(e){
const html = `Hello Laurence Svekis 2`;
return HtmlService.createHtmlOutput(html);
}
Share options are also available, just like other workspace apps, Apps Script can also
be shared and collaborated on. Edit permissions will allow others to edit the code, view
will only allow viewing of the code.
7
Bound script can add custom functions into a spreadsheet, run special triggers and
function to interact with the container file.
To open the editor menu, right click or Control+Click within the editor. This will open the
editor menu.
Bound scripts can add UI menus to a container file, within sheets using the UI object
add a custom menu.
1. Using the onOpen() run a block of code as the spreadsheet opens.
2. Select the Ui object and add several items to the menu. Create the functions,
use strings to represent both label and function names.
3. Create a couple of functions that will use the UI and open alerts with function
messages.
9
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('adv')
.addItem('test1', 'test1')
.addItem('test2', 'test2')
.addToUi();
}
function test1() {
SpreadsheetApp.getUi().alert('test1');
}
function test2() {
SpreadsheetApp.getUi().alert('test2');
}
To run code, add it to your apps script, and wrap the code within a function. This can
then be run with the Run button. Try out the LanguageApp service below.
function test3(){
const val = 'Hello World';
const spanish = LanguageApp.translate(val, 'en', 'es');
Logger.log(spanish);
}