Google drive code gives error when adding trigger [duplicate] - google-apps-script

Whenever I run the following function
function getDatabases(){
var files = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
var amount = 0;
while (files.hasNext()){
if((files.next().getName()).indexOf("Database") > -1){
amount++;
}
}
return amount;
}
I get this error:
This suddenly started about a month and a half ago. The same code was working earlier so I thought I reached a quota limit but that was not the case.

got my solution
In the script -> Resources -> Advanced google services -> turn on Drive API
Click in below message " Google Cloud Platform API Dashboard" to open de cloud project for the script. With the project selected, search in the search bar "Drive API" -> ENABLE, do the same for "Google Drive API".
Done, no more "we're are sorry server error" for DriveApp functions in the script.
Also: I enabled other APIs and advanced services Drive Activity API, just in case...

I ran into this issue too...
Like Nicolás Paulino mentioned, check your "Advanced Google Services" are turned on with the Apps Script Editor UI..
As stated...from the apps script editor..
select the "Resources" menu item
then "Advanced Google Services"
then find all/all related services and toggle them on.
Then head to ...
https://console.cloud.google.com/apis/library?project={yourProjectId}
Click on the menu (top-left)
hover on "Apis & Services"
click "Library"
search for any/all related APIs you're using

I've just had your same issue and it took me forever to fix it. What worked for me was to create a copy of the spreadsheet with the bound script. After authorising everything again in the copy, it worked. Hope it helps!

If you have connected your Apps script to Google Cloud Project then you have to enable the Drive API in your console.
Drive API: https://console.cloud.google.com/apis/api/drive.googleapis.com/
Else you can go to Apps script -> Services -> Add Drive API
Source: https://developers.google.com/apps-script/guides/services/advanced
Also beware that there is a quota limit for the number of times you have called the DriveAPI. If you are not connected to any GCP project then just create a new copy of spreadsheet and try again.

Related

A problem with Presentations from Apps Script

Today, when I was run SlidesApp batchUpdate, the Google AppsScript returned me the following error:
Exception: Service Google Slides API has not been enabled for your Apps Script-managed Cloud Platform project. If you have recently turned on this advanced Google service, wait a few minutes for the action to propagate to our systems and then retry.
I don't know what was happen. Yesterday, the Apps Script was running correctly.
Anyone can help me to solve this?
Google functionality is scoped to a Google Project.
Your Apps Script is associated with a Google Project. Any other Google services (APIs) that your Apps Script uses must be enabled in the same Project.
Replacing [PROJECT] with your Apps Script's Project, visit the following link that represents the Google Slides API:
https://console.cloud.google.com/apis/library/slides.googleapis.com?project=[PROJECT]
You can click the ENABLE button to enable the Google Slides API in the Project and thus for your Apps Script too.
NOTE You either changed Projects or added/disabled Google Slides functionality to your Apps Script but this would not have worked yesterday.

DriveApp Error: "We're sorry, a server error occurred. Please wait a bit and try again."

Whenever I run the following function
function getDatabases(){
var files = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
var amount = 0;
while (files.hasNext()){
if((files.next().getName()).indexOf("Database") > -1){
amount++;
}
}
return amount;
}
I get this error:
This suddenly started about a month and a half ago. The same code was working earlier so I thought I reached a quota limit but that was not the case.
got my solution
In the script -> Resources -> Advanced google services -> turn on Drive API
Click in below message " Google Cloud Platform API Dashboard" to open de cloud project for the script. With the project selected, search in the search bar "Drive API" -> ENABLE, do the same for "Google Drive API".
Done, no more "we're are sorry server error" for DriveApp functions in the script.
Also: I enabled other APIs and advanced services Drive Activity API, just in case...
I ran into this issue too...
Like Nicolás Paulino mentioned, check your "Advanced Google Services" are turned on with the Apps Script Editor UI..
As stated...from the apps script editor..
select the "Resources" menu item
then "Advanced Google Services"
then find all/all related services and toggle them on.
Then head to ...
https://console.cloud.google.com/apis/library?project={yourProjectId}
Click on the menu (top-left)
hover on "Apis & Services"
click "Library"
search for any/all related APIs you're using
I've just had your same issue and it took me forever to fix it. What worked for me was to create a copy of the spreadsheet with the bound script. After authorising everything again in the copy, it worked. Hope it helps!
If you have connected your Apps script to Google Cloud Project then you have to enable the Drive API in your console.
Drive API: https://console.cloud.google.com/apis/api/drive.googleapis.com/
Else you can go to Apps script -> Services -> Add Drive API
Source: https://developers.google.com/apps-script/guides/services/advanced
Also beware that there is a quota limit for the number of times you have called the DriveAPI. If you are not connected to any GCP project then just create a new copy of spreadsheet and try again.

Google sheets & google analytics authentication

I'm trying to use the real-time-data export for google analytics in google sheets scripts.
However my google analytics api/ developer console is under a different account as my google sheets account.
I specify the project my google sheets script should use in the following line:
var ana = Analytics.Data.Realtime.get('ga:xxxxxxxxx','rt:activeUsers, rt:source', {'dimensions' : 'rt:source,rt:medium'});
However this throws the error:
Access Not Configured. Google Analytics API has not been used in project 1046300490979 before or it is disabled.
When i open up this error and click the link provided it sends me to a different project then the project id i specify in the line of code above.
How would i go about getting google analytics data through the API of a different account?
P.S.: I have followed the following guide to set up my google sheets script: https://www.tatvic.com/blog/google-analytics-real-time-data-in-google-sheet/
EDIT: I found out that you can change the project connected to your script.
However after doing so i am getting the following error:
User does not have sufficient permissions for this profile. (regel 3, bestand 'Code')
In the developer console i have access to everything.
Turns out i was using the wrong id's so i'll elaborate a little on where to get what id's in the tutorial above.
Under Resources -> Cloud Platform-project you have to link your script to your google developer console.
You can find this id by going to your developer console, selecting the project connected to your google analytics account. There should be a projectID under Projectinformation
In the following line:
var ana = Analytics.Data.Realtime.get('ga:xxxxxxx', 'rt:activeUsers', {'dimensions' : 'rt:source,rt:medium'});
xxxxxxx should be replaced by your google analytics project id, open your google analytics and look at the URL it should be all numbers behind the p.
https://analytics.google.com/analytics/web/#/report-home/a75228945w113577272p128654983

How to enable Drive API using google Apps script in script editor

I am trying to enable Google Drive API using apps script so that we can upload any file to drive without manual intervention.
edit
function test()
{
var file = DriveApp.getFileById(newSpreadSheet.getId()); file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}
This is how I'm setting permission for a spreadsheet using code. I need a similar thing to enable drive API as well.So that anybody can use the spreadsheet without enabling the drive API manually from google developer console.
Thanks in advance
Enabling an API is usually done through the Google Developer Console as given in enabling Google APIs Services wherein you enable them from the Google APIs Services dialog box from the Script Editor then through the Google APIs Console link.
However, if you don't want to go through the Developer Console, you may want to try Connecting to public APIs which is applicable for Google (or non-Google) API that isn't available as an Apps Script service. As mentioned,
If you want to use a Google (or non-Google) API that isn't available as an Apps Script service, you can connect to the API's public HTTP interface through the URL Fetch service.
Hope that helps!

Unable to run BigQuery.Jobs.query in Google Spreadsheets

I'm constantly getting an "Access Not Configured" error despite having done everything the tutorial page has instructed.
Help!
There is a missing step in the tutorial.
Where it says:
Section 1: Activate the Google BigQuery service in Apps Script
Create a new spreadsheet.
Click Tools > Script Editor... to open the script editor.
Click Resources > Use Google APIs....
Enable the BigQuery API (v2), then click OK (no API Key is required).
It's missing step 5 and 6:
Click on "To use the selected APIs, enable them in the Google APIs Console."
Turn on "BigQuery API."
First Configured it on Google API Console
1. Go to Google API Console
2. Create New Project
3. In Services Tab Enabled a BigQuery API
then you can configured in Apps script steps that you followed