Exception when openById of Spreadsheet in Google App - google-apps-script

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.

Related

Apps Script sheet.getCharts() throws Service Error: Spreadsheets

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.

getAs(MimeType.PDF) or getAs('application/pdf') with spreadsheets

Has getAs(MimeType.PDF) or getAs('application/pdf') functionality been changed recently?
A sample of my code:
var blSheet = DriveApp.getFileById('templateID').makeCopy(title, destFolder);
var ss2 = SpreadsheetApp.open(blSheet);
var sheet2 = ss2.getSheetByName('Sheet Name');
sheet2.getRange("B9").setValue(valueHere);
sheet2.getRange("C10").setValue(valueHere);
sheet2.getRange("D11").setValue(valueHere);
sheet2.getRange("B9:B11").merge();
sheet2.getRange("C9:C11").merge();
etc...
Then adding this spreadsheet to an email using
blSheet.getAs(MimeType.PDF)
This worked fine up until a week ago. Now the attached pdf in my email is the blank template. When I look at the new spreadsheet in my google drive, it has the appropriately updated/merged cells.
Any help?
It seems to me that either getAs is taking the spreadsheet before it's cells are updated/merged, or it is taking the first "version" of the spreadsheet even after it's been updated.
Take a look at the documentation for sendMail()'s attachments option. It requires a BlobSource[] object array. It actually just accepts a Google Drive File without modification and attaches it as a PDF -- no extra work required.
Just give MailApp sendMail a File object.
The problem that you might be experiencing is that the changes made to the sheet are not fully committed.
You might need to call:
SpreadsheetApp.flush()
That will commit your changes to the drive file and hopefully solve the issue.

Google Apps Script Error: "You do not have permission to call openById"

I am trying to use a Google Script that I found on GitHub. (It is Posted Below.) It is supposed to merge data from Google sheets into a Google Doc. However, when I try to run the script I get an error: "You do not have permission to call openById". I have researched the problem and the information is conflicting. But, I have tried the following:
Manually run the function in script editor before adding the trigger to Google sheet.
Authorized the script to access my Google Docs data.
Set the triggers for the function to "On Form Submit"
Added the script to my Google Sheet and ran it from the menu as instructed: Merge --> Fill Template
I continue to get the error. I see that there are other people having the same problem, but the answers provided so far have not solved the issue. Any help would be greatly appreciated.
function doMerge() {
var selectedTemplateId = "1foobarfoobarfoobarfoobarfoobarfoobar";//Copy and paste the ID of the template document here (you can find this in the document's URL)
var templateFile = DriveApp.getFileById(selectedTemplateId);
var mergedFile = templateFile.makeCopy();//make a copy of the template file to use for the merged File.
mergedFile.setName("filled_"+templateFile.getName());//give a custom name to the new file (otherwise it is called "copy of ...")
var mergedDoc = DocumentApp.openById(mergedFile.getId());
var bodyElement = mergedDoc.getBody();//the body of the merged document, which is at this point the same as the template doc.
var bodyCopy = bodyElement.copy();//make a copy of the body
bodyElement.clear();//clear the body of the mergedDoc so that we can write the new data in it.

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.