Apps Script sheet.getCharts() throws Service Error: Spreadsheets - google-apps-script

I want to access the charts on a spreadsheet, with this
var imageSheet = SpreadsheetApp.openById(scriptProperties.getProperty('SHEET_ID')).getSheetByName(scriptProperties.getProperty('CHART_SHEET'));
var charts = imageSheet.getCharts();
When I try this, I get a notification that says
Service Error: Spreadsheets
I verified that it's definitely opening the correct sheet. This works on a copy of this sheet in my companies G Suitde domain. Could this be related?

The problem was that the charts were actually "empty" even though they appeared to be filled with data. This happens if you copy & paste charts from one sheet to another.

Related

Exception when openById of Spreadsheet in Google App

I implemented a Google Slide Add On which makes a copy of a linked spreadsheets and then opens it (to retrieve all charts in this sheet).
This is the code in question:
// id is the spreadsheet id
var sheetFile = DriveApp.getFileById(id);
var copy = sheetFile.makeCopy(sheetFile.getName() +'_'+ 'Copy');
Utilities.sleep(200);
// exception happens here
var sheet = SpreadsheetApp.openById(copy.getId());
Unfortunately this results for some sheets in the following error:
Exception: Service Spreadsheets failed while accessing document with id *************.
So I tried to find out why some spreadsheets work and some not and it lookls like that it does not work for not converted spreadsheets from excel. So I tested it by converting one of those excel sheets into a Google Sheet by Menu->File->Convert to Google Sheet.
This did not work... So I played around by replicated the Google Slide with the charts and I found out that I was not able to import the chart to a google slide from this converted spreadsheet. Even if I just create a new chart in this sheet everytime I go to the Slide App and try to insert the chart from this spreadsheet, no charts were displayed in the selection window.
It looks like that something went wrong when converting the excel to Google Sheets, I tried to do the conversion step multiple times... but no change.
[Edit]
I also tried to disabling V8 support.
[Edit2]
so for some reason I can now use SpreadsheetApp.openById(copy.getId()); when the spreadsheet was converted to google sheet. But as I mentioned before, it is still not possible to add any charts manually from this Google Sheet to presentation...
The error message Service Spreadsheets failed while accessing document with id ID you are receiving might in fact be related to this known issue on Google's side.
I suggest you star the issue here as all the updates regarding this will be posted there.
If it's a glitch I'd try to chain the commands this way:
var sheetFile = DriveApp.getFileById(id);
var copy_name = sheetFile.getName() +'_'+ 'Copy';
var sheet = SpreadsheetApp.openById(sheetFile.makeCopy(copy_name).getId());
This way the command openById won't fire before the file will be copied.

Google Script experienced an error only for other users (works fine for me)

I'm new to this and created a simple script to collect data with a save button.
It works fine for me as the developer, but other users get an error message:
"Script [name_of_script] experienced an error"
This is Google Apps Script is linked to simple Google Sheet, which is shared with several colleagues. They are to input some 10 data points, hit a 'save' button to save the data onto a 'data' tab (same Google sheets file) and clean the cells.
When I do hit the 'save' button it does as intended. When I tested it from another browser (not signed into my Google account), I get the above error message. The same applies to my colleagues (who also are not using a google account).
I thought this is to do with the Deployment, and I've tried all types of Deployment profiles, variating "Execute as" and "who has access" to no avail.
The forms and the sheets are un-protected (although I would like to protect them if all goes well).
As for 'Authorization' - I'm not sure what that means.
Many thanks for your help.
function submitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
//Input Values
var values = [[formSS.getRange("E26").getValue(),
formSS.getRange("E5").getValue(),
formSS.getRange("E7").getValue(),
formSS.getRange("E9").getValue(),
formSS.getRange("E11").getValue(),
formSS.getRange("E13").getValue(),
formSS.getRange("E15").getValue(),
formSS.getRange("E18").getValue(),
formSS.getRange("E20").getValue(),
formSS.getRange("E22").getValue(),
formSS.getRange("E24").getValue()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 11).setValues(values);
formSS.getRange('E5:E24').clearContent();
SpreadsheetApp.getUi().alert('Thanks')
}
You assigned a function from a spreadsheet bounded Google Apps Script to an image to be used as a button. The spreadsheet is shared with visitors (users not having a Google account). The visitors get an error message when they click the image. You tried different methods of deploying your script.
The script deployment is used for scripts to be used as Gmail, Google Calendar, Google Workspace Add-ons and Web Applications. A deployment hasn't an effect over functions called from container, in this case from the spreadsheet. Unfortunately there isn't a workaround that will make work calling a function by the spreadsheet visitors.
The most simple option to get data entry by "visitors" (users without a Google account) to be logged into a Google spreadsheet is to use a Google Form. Other "easy" options are to use Google Apps Script to create a web application and to use AppSheet.
Resources
Share documents with visitors

Permission Issue when moving data from one spreadsheet to another

I'm trying to write a script that moves a range of data from one spreadsheet to another. I'm running into a permission issue when I try to open the destination spreadsheet, I keep getting the error "You do not have permission to call open." I've tried open(file), openByUrl(url), and openById(id), and all three methods raise that same error.
I am the owner of the destination spreadsheet, so I don't understand why I don't have permission. I am also the owner of the script, and I have read/write access to the source spreadsheet (although I am not the owner).
Thanks for your help.
Here's my code:
function moveData() {
var source = SpreadsheetApp.getActiveSheet();
var destination = SpreadsheetApp.openById("1kfELXf9WxcZGsUO0piCjEXC_-ZR57GvbFzx1UPw89K4");
...
}
Unfortunately this behaviour is prohibited by Google: Following an update to Apps Script rolled out last June, custom functions are permitted to read data only from the current spreadsheet.
Their recommendation is to use the IMPORTRANGE formula instead.

Unable to access (an save) my SpreadSheet Google App Script

I have a Google SpreadSheet (filled through a form) associated to a Google Apps Script.
Now, the spreadsheet contains too many cells and I'm not able to load it anymore (server timeout).
I'm not interested in the data, but I really want to retrieve the script (the source code).
Unfortunately, the only way I know is to load the SpreadSheet first :-(
(Copying the Document ends in a timeout and the script is not copied.)
Any suggestion?
Regards,
RĂ©mi.
Create another script which makes a copy of original spreadsheet to backup the data and then clear the original spreadsheet through the script.
However this seems an issue which should be reported to Google Spreadsheet Forum
Here is an example code which will backup the spreadsheet as a copy and clear the original spreadsheet
function backupDataAndClear(){
var sourceSS = SpreadsheetApp.openById('ID_OF_ORIGINAL_SS');
var newSS = sourceSS.copy('Backup SS');
var sourceSheets = sourceSS.getSheets();
for(var i in sourceSheets){
sourceSheets[i].clear();
}
}

How to access data on different Google Spreadsheet through Google Apps Script?

I'm writing a Google Apps Script on my Google Site and am trying to use data that is provides on 2 different tabs in a Google Spreadsheet. From what I thought I understood from the documentation I could use all available methods in the SpreadsheetApp class on a Sites script by just using the openById() method.
Anyway here is what I tried to do
function doGet(e) {
var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE).getActiveSheet();
SpreadsheetApp.setActiveSheet(doc.getSheetId()[1]);
//....
}
I get the error
Cannot find method setActiveSheet(. (line 4)
I'm pulling my work off this link: Storing Data in Google Spreadsheets and also the Ui Service section listed under Building User Interfaces.
Anybody seeing what I'm doing wrong in these two lines?
setActiveSheet should be used only with the spreadsheet displayed by the UI, a sheet in a spreadsheet you have opened in your browser.
With SpreadsheetApp.openById you are opening a spreadsheet to access its data, but it doesn't open in your browser. It hasn't an UI.
I found this comments in https://developers.google.com/apps-script/class_spreadsheetapp?hl=es-ES#openById :
// The code below opens a spreadsheet using it's ID and gets the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Some examples assume your script is running into your spreadsheet. It's not your case, because you are running a script as a service, which should have its own User Interface.
I think #megabyte1024 addresses the syntax errors, but in answer to your comment to #YoArgentina:
Do you happen to know of a way to access data on different tabs then
through a service not running inside the Spreadsheet?
Does this sort of help?
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheet1A1 = sheets[0].getRange('A1').getValue();
var sheet2A1 = sheets[1].getRange('A1').getValue();
You need to access each sheet separately.
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheet = ss.getSheets()[0]; // "access data on different tabs"
ss.setActiveSheet(sheet);
There is at least one problem in these two lines. The 1st one is that the setActiveSheet method parameters is a Sheet class object and the getSheetId method returns an integer value. By the way this method (getSheetId) is not documented. The 2nd problem can happen if the SpreadsheetApp has no active spreadsheet. In this case there is the "Please select an active spreadsheet first." error. Use the SpreadsheetApp.setActiveSpreadsheet method to set an active spreadsheet.