Using Google Apps Script is a great way of controlling and automating Google Sheets, Google Docs, and even Google Data Studio. In fact, you can use it to interact with most apps in the Google ecosystem.
If it’s been a while since you’ve created or set up any Google Scripts, you might be surprised to learn that you can’t just create and go anymore. In fact, Google decided to change the process back in April 2019. In this article, we’ll talk about how you can go about setting up a Google Script from scratch, taking the ins and outs of the new process into account.
Accessing the Google Script editor
To get to the Google Script editor, navigate to Tools and select Script Editor.
At this point, you’ll need to write some code in order to create your Google App Script. If your Script interacts by pulling data from URLs or connecting to APIs, you’ll most likely encounter the below window when trying to debug or run the script.
If you click on ‘Review Permissions’, it’ll ask you for the account you want to continue with.
If you try and validate this with your Google account, it will fail with the below:
Authorising and running Google Scripts
This validation issue is due to Google Scripts no longer automatically authorising the Google Cloud Platform (GCP), meaning you need to manually set up a project and associate the Google Script with it to work. Find out more details around determining your project ID and enabling Script.
To authorise your Google Script, go to console.cloud.google.com and create a new project. You’ll then need to add the necessary credentials, all of which can be found in the above links. Then click the drop-down option next to Google Cloud Platform to open up the following window:
Click on the ‘New Project’ link:
Next, give the project a name. Note that this also becomes the project ID. While the project name can be changed at a later date, the project ID will stay fixed. It’s a good idea to use a single project for all your internal scripting needs, if you’re not worried about Google Script performance logging etc.
You’ll now need to set up the credentials for the project in the form of an OAuth client ID and add any consent screen.
Click the menu and go to API & Services to find Credentials:
At the credentials screen, click on Create Credentials and then Oauth Client ID:
You’ll now need to configure the consent screen by clicking Configure Consent Screen:
For non-G-Suite accounts, you can only select external:
Fill out the appropriate fields:
You’ll also probably want to validate your domain for extra validation:
Once you’ve gone through creating the consent screen, re-select Create Credentials and Oauth Client ID, as you’ll need to create Oauth 2.0 Client credentials:
Once complete, click Create and you’ll be presented with the Project Number, which you’ll need for the Script to run. Note, it is not the following number:
Linking your Google Apps script with your GCP project
After you’ve gone through all of the above, you’re finally ready to link your project to the Google App Script. Start by going into the Google Script Editor and select Resources, followed by Cloud Platform Project.
Once you’ve pressed Set Project, you should get a confirmation success message like the following:
As for the next steps, the following is a condensed summary of the instructions found on the Google Developer site:
1. If you don’t have a suitable GCP project, create a new one by following the Create a project instructions. Otherwise, open the GCP console Manage Resources page and locate an existing GCP project to use. You must have edit permissions for the GCP project and you must be able to view its settings in the Google Cloud Platform Console. The new project can’t be one that was automatically created by Apps Script.
2. Set a memorable GCP project name so that you can more easily locate it in the GCP console Manage Resources page. This name is also used to identify the script when Apps Script asks users to authorize it.
3. Determine the Project number of your GCP project.
4. In the Apps Script editor, open the script whose GCP project you want to replace.
5. Click Resources > Cloud Platform project.
6. In the Change Project section, paste the project number you copied into the text field, then click Set Project.
7. A warning screen explains the effects of changing the Cloud Platform project. Read the notice carefully, and click Confirm.
Re-test the App Script
The last step is to re-test the Google Script. This can be achieved by either debugging or running it:
The verification message you receive should be different. Upon opening the text, you will see ‘Go to [Your Project Name]’. Click it.
You’ll need to click allow.
Once you’ve gone through this whole process you should be able to run your Google App Script.
Each time you create a new Script, you can re-use the same Project Number created in Google Cloud Platform for authentication. You may want to create a different GCP project for scripts of different types, if you want to segment and monitor script performance more easily.
For even more helpful advice around using Google’s systems, check out the latest over on our blog, where you’ll also find pieces on paid search, SEO, and content marketing. Or why not look at some of our past campaigns, if you’re interested in taking your business to the next level?