Google Apps Script - A Beginner's Guide To Learn Apps Script
Google Apps Script - A Beginner's Guide To Learn Apps Script
Educator and Google Developer Expert. Let me help you with Google Sheets and Apps Script.
💡 Learn more
Learn how to write Apps Script and turbocharge your
Google Workspace experience with the new Beginner
Apps Script course
Google Apps Script is a cloud-based scripting language for extending the functionality of
Google Apps and building lightweight cloud-based applications.
It means you write small programs with Apps Script to extend the standard features of
Google Workspace Apps. It’s great for filling in the gaps in your workflows.
With Apps Script, you can do cool stuff like automating repeatable tasks, creating docu‐
ments, emailing people automatically and connecting your Google Sheets to other services
you use.
In this Google Sheets script tutorial, we’re going to write a script that is bound to our Google
Sheet. This is called a container-bound script.
(If you’re looking for more advanced examples and tutorials, check out the full list of Apps
Script articles on my homepage.)
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 1/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Let’s write our first, extremely basic program, the classic “Hello world” program beloved of
computer teaching departments the world over.
This will open a new tab in your browser, which is the Google Apps Script editor window:
By default, it’ll open with a single Google Script file (code.gs) and a default code block,
myFunction():
function myFunction() {
In the code window, between the curly braces after the function myFunction() syntax,
write the following line of code so you have this in your code window:
function myFunction() {
Browser.msgBox("Hello World!");
}
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 2/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Google Scripts have robust security protections to reduce risk from unverified apps, so we
go through the authorization workflow when we first authorize our own apps.
When you hit the run button for the first time, you will be prompted to authorize the app to
run:
Clicking Review Permissions pops up another window in turn, showing what permissions
your app needs to run. In this instance the app wants to view and manage your spread‐
sheets in Google Drive, so click Allow (otherwise your script won’t be able to interact with
your spreadsheet or do anything):
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 3/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
❗️ When your first run your apps script, you may see the “app isn’t
verified” screen and warnings about whether you want to continue.
In our case, since we are the creator of the app, we know it’s safe so we do want to contin‐
ue. Furthermore, the apps script projects in this post are not intended to be published pub‐
licly for other users, so we don’t need to submit it to Google for review (although if you want
to do that, here’s more information).
Click the “Advanced” button in the bottom left of the review permissions pop-up, and then
click the “Go to Starter Script Code (unsafe)” at the bottom of the next screen to continue.
Then type in the words “Continue” on the next screen, click Next, and finally review the per‐
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 4/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
missions and click “ALLOW”, as shown in this image (showing a different script in the old
editor):
More information can be found in this detailed blog post from Google Developer Expert
Martin Hawksey.
Once you’ve authorized the Google App script, the function will run (or execute).
If anything goes wrong with your code, this is the stage when you’d see a warning message
(instead of the yellow message, you’ll get a red box with an error message in it).
Return to your Google Sheet and you should see the output of your program, a message
box popup with the classic “Hello world!” message:
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 5/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Click on Ok to dismiss.
Great job! You’ve now written your first apps script program.
At present, it’s called myFunction which is the default, generic name generated by Google.
Every time I want to call this function (i.e. run it to do something) I would write
myFunction(). This isn’t very descriptive, so let’s rename it to helloWorld(), which gives us
some context.
function myFunction() {
Browser.msgBox("Hello World!");
}
to this:
function helloWorld() {
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 6/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Browser.msgBox("Hello World!");
}
Note, it’s convention in Apps Script to use the CamelCase naming convention, starting with
a lowercase letter. Hence, we name our function helloWorld, with a lowercase h at the start
of hello and an uppercase W at the start of World.
In its current form, our program is pretty useless for many reasons, not least because we
can only run it from the script editor window and not from our spreadsheet.
Let’s fix that by adding a custom menu to the menu bar of our spreadsheet so a user can
run the script within the spreadsheet without needing to open up the editor window.
This is actually surprisingly easy to do, requiring only a few lines of code. Add the following
6 lines of code into the editor window, above the helloWorld() function we created above,
as shown here:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('My Custom Menu')
.addItem('Say Hello', 'helloWorld')
.addToUi();
}
function helloWorld() {
Browser.msgBox("Hello World!");
}
If you look back at your spreadsheet tab in the browser now, nothing will have changed.
You won’t have the custom menu there yet. We need to re-open our spreadsheet (refresh
it) or run our onOpen() script first, for the menu to show up.
To run onOpen() from the editor window, first select then run the onOpen function as shown
in this image:
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 7/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Now, when you return to your spreadsheet you’ll see a new menu on the right side of the
Help option, called My Custom Menu. Click on it and it’ll open up to show a choice to run
your Hello World program:
An alternative way to run Google Scripts from your Sheets is to bind the function to a button
in your Sheet.
For example, here’s an invoice template Sheet with a RESET button to clear out the
contents:
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 8/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
For more information on how to do this, have a look at this post: Add A Google Sheets
Button To Run Scripts
Another great way to get started with Google Scripts is by using Macros. Macros are small
programs in your Google Sheets that you record so that you can re-use them (for example
applying standard formatting to a table). They use Apps Script under the hood so it’s a great
way to get started.
Read more: The Complete Guide to Simple Automation using Google Sheets Macros
Let’s create a custom function with Apps Script, and also demonstrate the use of the Maps
Service. We’ll be creating a small custom function that calculates the driving distance be‐
tween two points, based on Google Maps Service driving estimates.
The goal is to be able to have two place-names in our spreadsheet, and type the new func‐
tion in a new cell to get the distance, as follows:
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 9/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Copy the following code into the Apps Script editor window and save. First time, you’ll need
to run the script once from the editor window and click “Allow” to ensure the script can inter‐
act with your spreadsheet.
Let’s take a look at another simple use case for this Google Sheets Apps Script tutorial.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 10/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Suppose I want to save copy of some data at periodic intervals, like so:
In this script, I’ve created a custom menu to run my main function. The main function,
saveData(), copies the top row of my spreadsheet (the live data) and pastes it to the next
blank line below my current data range with the new timestamp, thereby “saving” a snap‐
shot in time.
See this post: How To Save Data In Google Sheets With Timestamps Using Apps Script, for
a step-by-step guide to create and run this script.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 11/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Google Apps Script is by no means confined to Sheets only and can be accessed from oth‐
er Google Workspace tools.
Here’s a quick example in Google Docs, showing a script that inserts a specific symbol
wherever your cursor is:
2. Open script editor from the menu: Extensions > Apps Script
3. In the newly opened Script tab, remove all of the boilerplate code (the “myFunction” code
block)
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 12/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
cursor.insertText('§§');
cursor.insertText('( ͡° ͜ʖ ͡°)');
6. Click Save and give your script project a name (doesn’t affect the running so call it what
you want e.g. Insert Symbol)
7. Run the script for the first time by clicking on the menu: Run > onOpen
8. Google will recognize the script is not yet authorized and ask you if you want to continue.
Click Continue
9. Since this the first run of the script, Google Docs asks you to authorize the script (I called
my script “test” which you can see below):
13. Click on Insert Symbol and you should see the symbol inserted wherever your cursor is.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 13/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Use the Logger class to output text messages to the log files, to help debug code.
The log files are shown automatically after the program has finished running, or by going to
the Executions menu in the left sidebar menu options (the fourth symbol, under the clock
symbol).
The syntax in its most basic form is Logger.log(something in here). This records the val‐
ue(s) of variable(s) at different steps of your program.
For example, add this script to a code file your editor window:
function logTimeRightNow() {
const timestamp = new Date();
Logger.log(timestamp);
}
Run the script in the editor window and you should see:
I’ve only scratched the surface of what’s possible using G.A.S. to extend the Google Apps
experience.
1) A Sheets/web-app consisting of a custom web form that feeds data into a Google Sheet
(including uploading images to Drive and showing thumbnails in the spreadsheet), then cre‐
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 14/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
ates a PDF copy of the data in the spreadsheet and automatically emails it to the users.
And with all the data in a master Google Sheet, it’s possible to perform data analysis, build
dashboards showing data in real-time and share/collaborate with other users.
2) A dashboard that connects to a Google Analytics account, pulls in social media data,
checks the website status and emails a summary screenshot as a PDF at the end of each
day.
3) A marking template that can send scores/feedback to students via email and Slack, with
a single click from within Google Sheets. Read more in this article: Save time with this cus‐
tom Google Sheets, Slack & Email integration
My friend Julian, from Measure School, interviewed me in May 2017 about my journey into
Apps Script and my thoughts on getting started:
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 15/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
How to get started with Google Apps Script (feat. Ben Collins)
For further reading, I’ve created this list of resources for information and inspiration:
Course
💡 Learn more
Learn how to write Apps Script and turbocharge your
Google Workspace experience with the new Beginner
Apps Script course
Documentation
Communities
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 16/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
For general Javascript questions, I recommend this JavaScript tutorial page from W3
Schools when you’re starting out.
When you’re more comfortable with Javascript basics, then I recommend the comprehen‐
sive JavaScript documentation from Mozilla.
Imagination and patience to learn are the only limits to what you can do and where you can
go with GAS. I hope you feel inspired to try extending your Sheets and Docs and automate
those boring, repetitive tasks!
Related Articles
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 17/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
April 16, 2021 Ben Apps Script Apps Script, dashboard, Google Sheets
Jane
June 1, 2016 at 10:32 am
Ben
June 1, 2016 at 3:06 pm
Thank you!
Rishu
March 14, 2018 at 4:53 am
Hey Ben,
Cannot read property “legs” from undefined. (line 11, file “Code”)
Can you please help
OldNerd
July 12, 2018 at 2:19 pm
@Rishu, were you able to fix it? I’m facing the same problem. Strangely enough it
doesn’t happen all the time, script works several times and then suddenly starts giving
the error. I have searched the web and it seems to be a recurring problem with no def‐
inite answer. Any help will be appreciated.
Regards
Jack
July 25, 2018 at 1:30 am
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 18/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
We need to have Google Cloud credential to enable the Maps API in order for us to
continue using the API
Ahmed
January 15, 2021 at 12:18 pm
Cromwel
November 6, 2018 at 5:57 am
Hi Ben,
How far did you get with project number 1 above. Kindly share a tutorial.
Letter Head
November 29, 2018 at 8:33 pm
Hi Ben – Awesome work! Like Cromwel, I would love to hear more on your project#1.
Thank you for sharing.
Nani
July 3, 2022 at 1:00 pm
Armando
July 12, 2016 at 7:08 pm
Hi, Im writing some code in google script, but when I open de file, my script doesn’t work
until the first minute i Think, its like there is a delay before the script really respond to my
calls on buttons, do you know why is this happening?
Ben
July 12, 2016 at 10:16 pm
Hi Armando,
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 19/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Hmm, there shouldn’t be any substantial delay, certainly not a minute. The script could be
affected by your internet connection speed, but I wouldn’t expect to see such a big delay,
especially if other web services load normally.
Inefficient code, i.e. lots of calls to/from the Google Sheets or other Google services, can
make a script run slowly, so it’s best practice to minimize the number of calls required.
More reading here at the official Apps Script docs: https://developers.google.com/apps-
script/best_practices
I suggest sharing your question/code on the Apps Script forum here: https://plus.google.‐
com/u/0/communities/102471985047225101769 where there’s a community of experts
who can suggest specific fixes.
Ben
vishal
September 28, 2016 at 8:45 am
hi, i want to track a user who copy, cut, or delete the more then 10 cells at once, please pro‐
vide me a code or a way how can i do it
Sat
October 26, 2016 at 2:58 am
Hi, Ben,
Thanks for very well written article that even a beginner like myself could understand! What
resources/training courses/tutorials on Google App scripts you would advise to a person
that have no previous knowledge of programming.
Thanks,
Sat
Ben
November 1, 2016 at 5:17 pm
Hi Sat, there’s a list of resources at the end of this post which is where I’d start. Then try
creating your own scripts and googling for answers when things don’t work. The forums
are very helpful at answering specific questions.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 20/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Cheers,
Ben
Marwane BERRIS
January 19, 2017 at 2:32 pm
Thx
Sebastian Marquez
February 17, 2017 at 4:42 pm
My script was doing the same thing until I opened up Google Maps
Ben
February 18, 2017 at 6:52 pm
Gary Miller
April 26, 2017 at 5:23 am
I found that if the location is one of several that would be returned from a Maps search,
an error is displayed. No real surprise there. You have to specify enough detail to make
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 21/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
it unique. Here in the UK I was searching for ‘Dover’, but until I put ‘Dover, Kent’ the dis‐
tance was not displayed.
Ben
May 2, 2017 at 10:22 am
Interesting, thanks for sharing Gary. I’ve never taken this function beyond this very ba‐
sic example for illustration, but clearly you could improve the error handling and give
this kind of feedback to a user. Cheers!
Stefano
February 15, 2017 at 8:16 am
Hi Ben,
I found the dashboard that connects to a Google Analytics account, pulls in social media
data, checks the website status and emails really amazing and extremely useful. Do you
plan to share it or to make a tutorial on how to built a dashboard like that?
Thanks,
Stefano
Ben
February 15, 2017 at 9:22 am
Hey Stefano,
That dashboard is covered in depth in the online dashboard course I’m launching….next
week (20th Feb)! I walk through how to create it step-by-step, including the apps scripting.
P.S. There’ll be a special offer for newsletter subscribers next week (sign up form at the
end of this page here).
Cheers,
Ben
Augustiine Agbi
February 27, 2017 at 7:47 am
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 22/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Very interesting. Am in dire need to learn how to develop a mobile app for e-learning/as‐
sessment. However, I don’t have knowledge of programming. I will appreciate if I can get a
step by step guide on programming. Thanks. Augustine
motuandpatlugames.in
March 27, 2017 at 7:11 pm
Gary Miller
March 30, 2017 at 9:19 am
I get an error: Cannot call Browser.msgBox() from this context; have you tried Logger.log()
instead? (line 2, file “Code”) when I try to run the Hello World script. Failing at the first at‐
tempt does not feel good. Do you know what the problem is?
Ben
March 30, 2017 at 4:00 pm
Hmm, not sure. Did you get the Authorization Required popup? What’s the exact error
message you’re getting? The popup will show up in your Sheet browser tab, not the code
editor tab, so you need to return to that one to the Hello World message.
Gary Miller
March 31, 2017 at 5:30 am
I did not get the authorisation pop up when I ran the script. However, i have tried it today
and hit the debug icon, instead of run. This DID produce the authorisation pop up, and
the function has run fine. Curious behaviour!
Ben
March 31, 2017 at 10:50 am
Soren Gran
September 1, 2021 at 4:53 pm
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 23/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
I also encountered this error. My issue is that I created my script in Google Apps Script,
so my script wasn’t directly tied to any Google Sheet. To fix this, I went to the Google
Sheet in which I wanted this code to run, clicked on the Tools dropdown menu, and
clicked on “Script editor”. This took me to a new Google Apps Script project that was di‐
rectly tied to that Google Sheet. I was able to run this code successfully by following
these steps.
Pingback: How to build a real-time sales dashboard for E-junkie with Google Sheets and
Apps Script
Robert L. Crocker
June 26, 2017 at 2:19 pm
e.g.
var ui = SpreadsheetApp.getUi();
ui.createMenu(‘My Custom Menu’)
.addItem(‘Say Hello’, ‘helloWorld’)
.addToUi();
var ui = SpreadsheetApp.getUi().createMenu(‘MyMenu’)
.addItem(‘Hello There’, ‘helloWorld’)
.addToUi();
and it all worked fine.
Have no idea what the difference is.
I’ve had this kind of thing happen before in app script though…
in particular when I was testing for results less than a particular variable using var1 0 and
everything worked OK.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 24/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Nihar
June 29, 2017 at 1:42 am
Great Post
Thanks a lot it helped me a lot
I am also going to share it to my friends and over my social media.
Also,
Hackr.io is a great platform to find and share the best tutorials and they have a specific
page for Google apps script
Shilpa
July 5, 2017 at 1:12 am
Hi,
We have a container bound script and would like to password protect the Script Editor.Is
there any way to do this or a workaround available to ensure that only the developer can
access the Script Editor?
Masi
June 3, 2020 at 3:30 am
I’m looking for the answer too! and I can not find it after 3 years. Is there any way?
Mike T
August 23, 2017 at 2:34 pm
Even when I put the old values back in, it would no longer work.
Any suggestions?
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 25/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Ben
August 28, 2017 at 11:31 am
Hey Mike,
It’s most likely caused by ambiguity in the leg, which this basic function isn’t able to deal
with.
Cheers,
Ben
Mike T
August 28, 2017 at 1:24 pm
The strange thing is that even the two points that initially worked and returned the dis‐
tance, also stopped working after I tried other destinations. It only worked the first time
and then everything seemed to break.
Steven De Roover
September 4, 2017 at 6:30 am
What a lovely article. I’m just starting to explore all the possibilities of Google App Scripting.
I don’t have any main goal yet, so I’m just fiddling around a bit.
For starters I’d like to know whatever is possible with an app inside a google site, and I
didn’t find any documentation on how to implement custom UI controls on the properties on
the embedded app.
To clarify: I add a script in my google site, then on a page I add a “Google Script App”, and
add that script I just made. Then for that added ‘widget’, you can click on the edit button. I’d
like to know how to add my custom settings on that page.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 26/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Ben
September 5, 2017 at 4:38 pm
Hey Steven,
I’ve never used Google Sites, so I’m not sure the answer to your question. I’d suggest
posting your question to the Google Apps Script forum where I’m sure someone will have
some input: https://plus.google.com/u/0/communities/102471985047225101769
Cheers,
Ben
Pingback: Community Connectors: Native data connectors for Google Data Studio
Pingback: How to add a hyperlinked index sheet to your large Google Sheet workbooks -
Ben Collins
Brian Pugh
November 21, 2017 at 12:53 pm
I am new to scripting. I have 12 functions in a single script. I cannot find a suitable answer
to this question:
Ben
November 28, 2017 at 12:52 pm
Hey Brian,
function runFuncs() {
// put your 12 functions in here...
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 27/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
func1();
func2();
// etc...
}
function func1() {
Logger.log("Hello!")
}
function func2() {
Logger.log("Goodbye!");
}
where func1 and func2 would be the first two of your 12 functions. When you run run‐
Funcs(), it will execute those functions inside of it, func1 and func2 in this case, but could
be all your 12.
Ben
Abdul Sami
December 7, 2017 at 6:12 am
I am a new user to google sheets and I love working with excel automation and advance
excel. I searched about google sheet programming, and your this article become very ben‐
eficial to me.
Great work!
Ben
March 14, 2018 at 12:05 pm
Thanks Abdul!
Pingback: Slow Google Sheets? Here are 27 techniques you can try right now
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 28/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Gert-Jan M. Stoop
August 3, 2018 at 4:56 pm
Varun
August 13, 2018 at 2:14 am
Hi Ben, I am using getcharts() to retrieve and save a manually produced chart in google
sheets. I keep getting a google server error ‘We’re sorry, a server error occurred. Please
wait a bit and try again. (line 4, file “Code”)’. The code is below – can you please help me
with what I am doing wrong? Is it because the chart was not created with GAS in the first
place?
function myFunction()
{ var spreadsheet1=SpreadsheetApp.getActive();
var sheet = spreadsheet1.getSheets()[0]; // grabs active sheet
var chartvarun = sheet.getCharts(); // grabs the first chart on the sheet
var file = DriveApp.createFile(chart.getBlob()); // saves it as a file
}
Ike
September 13, 2018 at 5:09 am
@Varun – were you able to solve this? I am getting the same error. Strangely, I am using
the identical code on two sheets and only get an error on one.
Ike
September 13, 2018 at 5:22 am
@Varun or Ben – did this problem ever get resolved? I’m having the same issue and can’t
find the solution online. Thanks!
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 29/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Serena Alt
August 19, 2018 at 9:47 am
Hi Ben,
I am a junior programmer and my company has asked me to do some google docs work
that I have never touched before. Since we are a small company I would love to hear your
input…
Can you advise how I would export cvs data from an excel sheet/url to a google doc? I
need it automated to occur once a week.
Thanks!
Serena
Dario
August 31, 2018 at 5:36 am
I have search for a language refernece card for keywords, srtuctures and I could not find
yet but some fragments. Any idea where I could find a refcard, a cheat card, thank you
Abraham Felix
August 31, 2018 at 1:40 pm
Hello i tried to code the example but i keep gettign this error:
Cannot call Browser.msgBox() from this context; have you tried Logger.log() instead? (line
2, file “Code”)
—————————————————————————-
function myFunction() {
Browser.msgBox(“hello”);
}
Kevin Bartlett
November 29, 2018 at 7:41 pm
I get the same error as Abraham Felix: ‘Cannot call Browser.msgBox() from this context;
have you tried Logger.log() instead? (line 2, file “Code”)’.
The Authorization window DID pop up, and I even got an email from Google saying “Hel‐
loWorld was granted access to your Google account”, but I still get the ‘Cannot call
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 30/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Browser.msgBox’ error.
As someone pointed out above, getting an error in “Hello World” does not feel
encouraging.
Ben
November 30, 2018 at 10:26 am
Hey Abraham/Kevin,
Are you running this script from the code editor of a Google Sheet? The browser class
is only available to Google Sheets, not Docs, Sites, Forms etc.
I just tested it again for me and it works, so not sure why it’s not for you.
function myFunction() {
Logger.log("Hello World!");
}
and then go to the menu View > Logs in your script editor window to see the message.
Cheers,
Ben
Pingback: Macros: Add the power of automation to your Sheets without any code
HeavyDuty
December 4, 2018 at 1:17 pm
Fernando
December 6, 2018 at 9:23 am
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 31/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
I was reading your post and I really enjoyed it. One quick question. Is there any way to
overwrite the same file on Google Drive? I have been reading about it, but everybody men‐
tions that Google uses IDs and that’s why is not an easy way to overwrite a file.
Thanks for your post.
Jamie Anderson
December 15, 2018 at 6:02 pm
I just found this post and I am so excited to use this for my students. I usually ran multiple
mail merges within Word but this is going to save me time since my gradebook is in
Google!! Do you have any more articles specifically for teachers?
Ben
December 15, 2018 at 7:27 pm
Andrew Roberts
December 18, 2018 at 10:33 am
Ben
December 19, 2018 at 11:53 am
Hey Andrew,
Haha
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 32/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
hello folks,
where can i found a place with the listed functions of google script?
for now i just wanted one that activated my already saved filter views.
ramsey
January 11, 2019 at 2:21 pm
Absolutely awesome, Ben. I know I am late to game vs. the original date of this post, but I
am using automation more and more in my worksheets and appreciate the extra time you
take explaining how GAS comes together. I aspire to one day do the kind of work reflected
in that dashboard! ::drool::
Matty
March 15, 2019 at 12:24 pm
I know this is late for me to comment now or whatever, but for some reason when I get on a
project, where the script is supposed to be is blank. Tools and everything is there but the
place for me to write scripts are blank and I can’t do anything on it.
Steve Prescott
March 25, 2019 at 11:20 pm
Great example, I just wish it worked. There must have been a change since it was posted.
I get the following error “Cannot call SpreadsheetApp.getUi() from this context. (line 2, file
“GetCode”)”
Anyone know what to do to fix this? I just want to write something to my spreadsheet ;-(
Michael Barnard
June 29, 2019 at 8:59 am
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 33/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Pingback: Uso de los comandos Slack Slash para enviar datos de Slack a hojas de Google
– Down.co.ve
Pingback: Uso de los comandos Slack Slash para enviar datos de Slack a hojas de Google
– stips
Emily
April 15, 2019 at 12:40 am
Hello! Amazing tutorial. I am trying to run the script for List Growth History, and am getting
the following error “TypeError: Cannot call method “forEach” of undefined.” What does this
mean and how can I fix it?
Best,
Emily
Hey, thanks for the article. searched long google apps script guide. Finally found this blog
helpful.
Thanks
Zafar
July 7, 2019 at 2:32 pm
“Google script to activate the cell on button click. Button activates the cell in which it is
placed.”
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 34/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
I have a google sheet in which there is one button in one cell of each row. Each button calls
a macro to insert a row below the active cell. The problem is that when I click a button it in‐
serts the row below the active cell. Actually I want that when a particular button is clicked,
that particular cell gets activated in which that button is placed. If it happens, a new row will
be inserted only below the clicked button not below the active cell somewhere else.
Em Yusoff
August 8, 2019 at 10:48 am
Hi Ben, I’ve been following your class on Google Sheet Functions and the video tutorials
are quite self explanatory — like it so much. What kind of screen casting software you’re
using for those videos… Tq!
Ben
August 9, 2019 at 6:50 pm
John Williams
August 8, 2019 at 11:52 am
Ben
August 30, 2019 at 2:30 pm
Thanks, John!
Ajay
August 30, 2019 at 8:55 pm
Pingback: Google Sheets Macros: Add automation to your Sheets without any code
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 35/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Attila
September 3, 2019 at 5:16 am
Hi!
How do I create a button that I click on will save it to a specific location and give it a name
and date Thank you!
Sagan Siniard
September 11, 2019 at 11:46 am
Hey, I’m trying to design a script for when you present the slideshow, it changes a cursor to
a specific picture you want with a hotspot (the pixel where the cursor clicks).
Infiniti
September 13, 2019 at 2:58 am
Hi Ben,
Do you have a tutorial or the codes for the first Real World sheet/web-app example which
you mentioned above?
Pingback: How to connect the Strava API with Google Sheets and Data Studio
Jet
October 17, 2019 at 11:29 pm
iongorepro
October 31, 2019 at 6:13 pm
When we open the Script Editor from within a Google document for the first time, it auto‐
matically creates a new “Untitled project” for us. Let’s change that to something a bit more
descriptive, like “Create Headers.
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 36/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Numan Zahid
November 30, 2019 at 8:14 am
I have a question !
If I am using Map Service within GoogleSheet using Script Editor for calculating Distance
b/w two locations , am I gonna pay for it to google or its free ?
Ray
December 14, 2019 at 3:05 pm
I can’t even get “Hello World” to print… it just says “Running Function myfunction… Cancel
Dismiss” and never progresses beyond that. I did authorize it as described in the article… it
seems so simple yet I can’t even get this simple function to work. Any ideas?
Pingback: Google Sheets Sort By Color And Google Sheets Filter By Color, With Apps
Script
ddddd
January 29, 2020 at 11:17 pm
if column E has “get fish” then the email should send, – is this part correct? : –
function CheckSales() {
// Fetch the monthly sales
var monthSalesRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“myfish”).getRange(“E1:E1000”
);
var monthSales = monthSalesRange.getValue();
// Check totals sales
if (monthSales = “get fish”){
// Fetch the email address
var emailRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”).getRange(“B2”);
var emailAddress = emailRange.getValues();
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 37/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Sherri Watkins
March 2, 2020 at 8:36 am
Great guide!!
I do have a question… We are currently converting from Microsoft Office to Google Suite.
We create a TON of documents and have to share them within our organization and outside
the organization. Is there a way, when writing script, to have it not ask for authorization to
run the script? We have over 700 people that may use these documents and it would be
time consuming to have to do this for each file. Most of our documents have script of some
kind. Thank you!
ramesh
June 4, 2020 at 9:54 am
Allen
July 10, 2020 at 4:21 pm
You lost me at step one: How do I get to this? “Then click the menu Tools > ” I’m using
Chrome I made a new tab… then?
Ben
July 12, 2020 at 3:32 pm
Cheers,
Ben
Sofus Axelsen
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 38/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
September 25, 2020 at 4:46 am
Amazing article to read!
I’m wondering, what are the other functionalities of this Google App Script? Can I build a
web-app using GAS? It seems that most people don’t even know if this program exists. Or
I’m the only one who should have known this earlier?
Thank you in advance, Ben!
Your website is very beautiful, I like the theme and the style you’re using:)
zeta
October 9, 2020 at 10:39 pm
Good stuff
Duc
November 6, 2020 at 3:03 am
Hi Ben,
I have tried to practise your first example “Hello World” in Google sheet (my browser using
is Google Chrome).
But when I run the script, it shows the ReferenceError: Browser is not defined (line 2, file
“Code”). I also tried with ‘logger.log’ (instead of ‘browser.msg’) but the same error.
Much appreciated.
Duc
Binu K Mathew
November 8, 2020 at 1:34 am
Sagin
March 17, 2021 at 6:26 am
Nice
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 39/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
James waller
May 12, 2021 at 8:44 am
This is the best guide on Google Apps Script. After read this article, i can solve my error
and run my project on Google Apps Script. I will share this article with my friends. It will be
very helpful to all readers. Good job!
Ben
May 12, 2021 at 5:03 pm
Thanks, James!
Gilbert
May 31, 2021 at 11:07 am
Hey Ben!
I am enrolled in two of your great courses and I have been learning a lot! I am a beginning
into Apps Scripts and I have a question in a problem where I am stuck.
What I am doing with the code below is copying and paste the formula from one cell to the
next cell to the right. I am repeating this process in 30 rows. However, I need to tell the
code to skip copying and pasting the formula if the row is filtered or hidden and I am unable
to make it work.
function fillFormulaDown() {
const sheet = SpreadsheetApp.getActiveSheet();
let currentCell = sheet.getActiveCell();
if(currentCell.isRowHiddenByFilter != true){
while (currentCell.getValue()) {
let formula = currentCell.getFormulaR1C1();
if (formula) {
currentCell.offset(0, 1).setFormulaR1C1(formula);
}
currentCell = currentCell.offset(1, 0);
}
}
}
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 40/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
ankita
June 7, 2021 at 1:48 am
Hi
I want to link 3 different row status with a fourth one which is the master status for all the 3
status. I have used this following code but it’s only linking with one row and not with the oth‐
er 2
=ifs(D3=”YTS”,”Wireframe YTS”,D3=”WIP”,”Wireframe WIP”,D3=”RTK”,”Wireframe
RTK”,D3=”Review”,”Wireframe Review”,D3=”Correction”,”Wireframe
Correction”,D3=”Closed”,”Wireframe Closed”,H3=”YTS”,”Storyboard
YTS”,H3=”WIP”,”Storyboard WIP”,H3=”RTK”,”Storyboard RTK”,H3=”Review”,”Storyboard
Review”,H3=”Correction”,”Storyboard Correction”,H3=”Closed”,”Storyboard
Closed”,L3=”YTS”,”FCD YTS”,L3=”WIP”,”FCD WIP”,L3=”RTK”,”FCD
RTK”,L3=”Review”,”FCD Review”,L3=”Correction”,”FCD Correction”,L3=”Closed”,”FCD
Closed”)
Sonu Raj
June 7, 2021 at 2:38 pm
Kanhaiya
August 19, 2021 at 1:36 am
Hi Ben,
You are awesome. I have a question to you. I have created a script to get all filename and
URL using DriveApp and put the same in spreadsheet but after 1800 seconds (30 minutes)
script has stopped its execution with timeout error.
How to handle this if script will take more time in execution what to do to get the required
results ?
Pls help
Thanks in advance
Fathma Siddique
October 25, 2021 at 2:00 pm
Is there any way I can hide top bar default options like file, help etc?
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 41/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
Mark
February 5, 2022 at 4:40 pm
You are a life Saver, Ben. Just weeks ago, I discovered the awesome potential of Google
AppScript, and just now I’m on the lookout on how to learn the code, and by far this is the
best intro out there. I am specially fond of your mention of Google Docs, because now I
have new things to try there.
Ben
February 6, 2022 at 3:08 pm
After this article, check out my free Apps Script course: Intro to Apps Script
Hilton Fernandes
March 2, 2022 at 8:51 am
Congratulations, Ben !
Ben
March 2, 2022 at 9:39 am
Thanks, Hilton!
Raphael
March 3, 2022 at 6:59 am
This looks great! I want to start making a container-bound script inside a sheet, but I cant’t
find the “script editor” in the “tools” menu… Am I missing something? Probably a very
noobish beginners question: 😀
Ben
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 42/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
March 3, 2022 at 8:38 am
Hi Raphael,
It’s moved to the Extensions menu now! Sorry about that. They changed it a couple of
months ago and this article needs to be updated. Doing that next!
Cheers,
Ben
Tara Benavides
March 29, 2022 at 1:26 pm
Hi, I can get to AppsScript under extensions and open the editor there, but in Google
Sheets, under “Tools” there is no “Script Editor” option. Is there something I have to install
first?
I read that if you are using an organizations Google Suite, it has the ability to disable the
“Script Editor” function, but I don’t know if that applies. I am using a company
computer/browser, but the actual Google account is my own. Any idea what is going on?
Ben
March 29, 2022 at 1:38 pm
Hi Tara,
No, you’ve found it. They recently moved the Apps Script menu location from Tools to
Extensions. Unfortunately, some of my posts are now out-of-date. I will update this one
now.
Cheers,
Ben
Phil
April 3, 2022 at 1:36 pm
I get script funcion not found on my Adding a custom menu in Google Apps Script attempt.
Checkit over and over – any clues?
Ranjeet Kumar
April 20, 2022 at 12:37 pm
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 43/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
I just started the app script in google excel. But when I am trying to update the existing val‐
ue then it is not updating. My code is :
function updateData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName(“Form”); //Form Sheet
var datasheet = ss.getSheetByName(“Data”); //Data Sheet
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 44/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
datasheet.getRange(INT_R, 1, 1, 23).setValues(values1);
SpreadsheetApp.getUi().alert(' "Data Updated "');
return row[RETURN_COL_IDX];
}
}
}
Nick Thiele
April 27, 2022 at 5:58 pm
Do you know if it’s possible to write a Google script where you can upload a CSV file of ad‐
dresses you’ve SENT to, and then have the script filter that list and give you a new CSV file
that only includes the addresses that haven’t replied?
parisa
July 11, 2022 at 2:03 am
Hi,
I need to run 3 scripts for same sheet simultaneously in Google app script. can you please
help me?
Nolan Gutierrez
September 4, 2022 at 7:52 pm
This is interesting to me as a small-time researcher. It seems like a tool for hobbyists and
people who are interested in improving their productivity, possibly as small business own‐
ers, as opposed to being used for deployment. I wonder how many people have actually
used google apps scripts. I would be interested in the actual statistics. I just started by cre‐
ating some simple scripts to organize my debts/credits and to send emails. I think it’s a fun
tool to use and I think that it has a very high potential for scripting.
Sarah Aziz
December 1, 2022 at 8:03 am
I need help.
I was handed an app created by app script. Apart from the fact that I don’t understand App
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 45/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
script how can I add it online on our domain so that it can be accessed any where.
I want the app to be connected to mysql database, is it possible?
Jonah
February 28, 2023 at 2:46 am
Steph Andrusjak
April 16, 2023 at 12:06 pm
Apps Script is so powerful. I’ve only dabbled so far and been impressed on how it can help
me drill down the data for easier analysis on Google Sheets. Thanks for sharing these
posts and guides on your website, Ben, and for your very informative newsletter which I al‐
ways enjoy reading!
Givie Castanares
June 8, 2023 at 8:27 am
Hi! Sir Ben! I’m such a big fan of all your work. I just want to say thank you for keep sharing
your idea to everyone <3
Wish you always in good health (bow)
Sean Power
January 5, 2024 at 12:53 am
Love your work, this was very easy to follow and a goood taster for what could be learned
with some of the courses on offer here. Thanks again, this was great.
Ben
January 5, 2024 at 12:22 pm
You’re welcome! Thanks for reading and good luck on your Apps Script journey!
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 46/47
28/05/2024, 13:45 Google Apps Script: A Beginner's Guide To Learn Apps Script
https://www.benlcollins.com/apps-script/google-apps-script-beginner-guide/ 47/47