Script stops working when I duplicate a google sheet - google-apps-script

I've created an order form using a google sheet, with a script to email a pdf of the order to the supplier. I tested it and it worked perfectly fine, so I saved it to the template gallery.
Now when I create a new google Sheet from the template and run the script I get an error - TypeError: Cannot call method "getSheetId" of undefined.
Now if I duplicate the script on the new spreadsheet I just created and call on the duplicated code, it works. Strange thing is when I now try and call the original code, this now works as well. Even stranger when I now delete the duplicated script I just made, the original code stops working again.
I've done this multiple times and I get the same result.
Has anyone got any ideas? Am I doing something completely wrong here?
function emailOrder(){ // this is the function to call
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
var shName = sh.getName()
var email = sh.getRange('I8').getValue();
var dates = sh.getRange('V8').getValue();
var reference = sh.getRange('V5').getValue();
var vendor = sh.getRange('B5').getValue();
var contact = sh.getRange('B8').getValue();
sendSpreadsheetToPdf(shName, email , vendor + ' - '+ dates + ' - ' + reference , "Hi " + contact + ", <br /> <br /> See attached order for reference: " + reference + ", <br /> <br /> Kind regards, <br />NAME<br /> PH NUMBER <br /> <br />");
};
function sendSpreadsheetToPdf(pdfName, email, subject, htmlbody) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId();
var url_base = spreadsheet.getUrl().replace(/edit$/,'');
var sheetId = spreadsheet.getActiveSheet().getSheetId();
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (('&gid=' + sheetId ))
// following parameters are optional...
+ '&size=A4' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&top_margin=0.4&bottom_margin=0.4&left_margin=0.4&right_margin=0.4'; // Margins: set all to 0.4
var options = {
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {attachments:blob, htmlBody:htmlbody}
MailApp.sendEmail(
email,
subject,
"html content only",
mailOptions);
}
}

There are two types of Apps Script documents - Standalone scripts and Bound scripts.
The standalone scripts are not bound to a specific document and are opened and called separately from a document. Thus, if you want call a spreadsheet or sheet within a standalone script, you need to do so by opening the spreadsheet by id or url and the sheet by name
Bound scripts are bound to a specific document ad they can call the bound spreadsheet and its sheets with SpreadsheetApp.getActiveSpreadsheet() and spreadsheet.getActiveSheet(). Bound scripts technically be called separately from https://www.google.com/script/. However, doing so can lead to errors if the bound sreadsheet is not open or is open multiple times. Instead, bound scripts should be open from the UI of the bound document through Tools->Script Editor. Goog practice is to have only one version of the spreadsheet opened, and this recently, before calling the script. Otherwise the determination of the active spreadsheet and sheet can lead to errors.

Related

urlfetchapp.fetch <url> are not permitted ERROR in Google Apps Script

I had a Google Apps Script code bound to a Google Spreadsheet that created a PDF file from one of its sheets using the urlfetchapp.fetch(url, options) function. Yesterday, without editing the code, it suddenly stopped working, showing the error "urlfetchapp.fetch are not permitted".
I checked other spreadsheet bound code I made for other Spreadsheet and it is not working anymore either, same error.
For context, I am using Google Workspace for my company, and dealing with sensitive data. But it was working just fine up until yesterday.
function CrearPDFysubirloalDrive(){
var Ss = SpreadsheetApp.getActiveSpreadsheet();
var Sheet= Ss.getSheetByName("Name")
var Longitud = Sheet.getLastRow();
var range = Sheet.getRange(1,1,Longitud,10);
var docId ="Planning";
var docId1Semana = Sheet.getRange('F5').getValue();
var docIdRevision = Sheet.getRange('C7').getValue();
var pdfname= docId+ " W"+docId1Semana+"-Rev "+docIdRevision;
//This is not important to the question
var tempst= Sheet.copyTo(Ss).setName(pdfname);
tempst.insertColumns(1);
tempst.setRowHeight(1,30)
tempst.setColumnWidth(1, 20);
tempst.setColumnWidth(12, 30);
tempst.deleteColumns(13,tempst.getMaxColumns()-12)
//tempst.deleteRows(Longitud+2, (tempst.getMaxRows()- (Longitud+2)))
tempst.getRange("A4:A5").setBackground("white");
var drawing = tempst.getDrawings();
for (let i=0; i<drawing.length;i++){
drawing[i].remove();
}
SpreadsheetApp.flush();
//THIS PART IS GIVING THE ERROR
var url = 'https://docs.google.com/spreadsheets/d/{ID}/export?'.replace('{ID}', Ss.getId());
var exportOptions = 'exportFormat=pdf&format=pdf' + // export as pdf / csv / xls / xlsx
'&size=letter' + // paper size legal / letter / A4
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&top_margin=0.40' + //All four margins must be set!
'&bottom_margin=0.00' + //All four margins must be set!
'&left_margin=0.40' + //All four margins must be set!
'&right_margin=0.40' + //All four margins must be set!
'&gridlines=false' + //true/false
'&gid=' + tempst.getSheetId(); // the sheet's Id
var token = ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url + exportOptions, {
headers: {
Authorization: 'Bearer '+token
}
}).getBlob().setName(tempst.getName()+".pdf");
var pdfFile = DriveApp.createFile(blob);
var FolderDestino= DriveApp.getFolderById(<FOLDERID>);
FolderDestino.createFile(blob.setName(pdfname))
I am not an expert at coding by any means, but I suspect it is related to the Oauth token. Either that or the function has been deprecated.
EXACT ERROR CODE:
Exception: UrlFetch calls to https://docs.google.com/spreadsheets/d/*******/export?exportFormat=pdf&format=pdf&size=letter&portrait=true&fitw=true&source=labnol&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false&top_margin=0.40&bottom_margin=0.00&left_margin=0.40&right_margin=0.40&gridlines=false&gid=2141838874 are not permitted.
Any help?
Thank you in advance
It seems like there is a bug
The issue has already been filed on Google's Issue Tracker. I recommend you to "star" it to increase visibility.

Save to PDF script in Google Sheet

I already had a look to the community but I'm struggling in building a script should do the following:
select a specific range of cells of a Google Sheet sheet
save the selected area as a PDF to be downloaded on the PC (no need to save to GDrive or to add a specif name to the PDF)
I tried recording a Macro but dosn't work.
Can you give some hints on how to move on?
Thanks.
These reference links may help you to have some hints on how you will achieve your target:
This first link shows you how to download a range of cell as PDF:
Script to download a range of cells in google sheet as PDF to local computer and other automation scripts?
Script that exports a range to PDF without borders in reference to the first link and answered by ZektorH:
function downloadRangeToPdf() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:E20");
//Create temporary Spreadsheet
var tempSpreadsheet = SpreadsheetApp.create("tempSheetInvoiceExport", range.getValues().length, range.getValues()[0].length);
var tempSheet = tempSpreadsheet.getSheets()[0];
var tempRange = tempSheet.getRange("A1:E20");
tempRange.setValues(range.getDisplayValues());
tempRange.setTextStyles(range.getTextStyles());
tempRange.setBackgrounds(range.getBackgrounds());
tempRange.setFontColors(range.getFontColors());
tempRange.setFontFamilies(range.getFontFamilies());
tempRange.setFontLines(range.getFontLines());
tempRange.setFontStyles(range.getFontStyles());
tempRange.setFontWeights(range.getFontWeights());
tempRange.setHorizontalAlignments(range.getHorizontalAlignments());
tempRange.setNumberFormats(range.getNumberFormats());
tempRange.setTextDirections(range.getTextDirections());
tempRange.setTextRotations(range.getTextRotations());
tempRange.setVerticalAlignments(range.getVerticalAlignments());
tempRange.setWrapStrategies(range.getWrapStrategies());
SpreadsheetApp.flush(); //Force changes to be written before proceeding.
//Generate Download As PDF Link
var url = 'https://docs.google.com/spreadsheets/d/{ID}/export?'.replace('{ID}', tempSpreadsheet.getId());
var exportOptions = 'exportFormat=pdf&format=pdf' + // export as pdf / csv / xls / xlsx
'&size=letter' + // paper size legal / letter / A4
'&portrait=true' + // orientation, false for landscape
'&fitw=true&source=labnol' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&top_margin=0.00' + //All four margins must be set!
'&bottom_margin=0.00' + //All four margins must be set!
'&left_margin=0.00' + //All four margins must be set!
'&right_margin=0.00' + //All four margins must be set!
'&gridlines=false' + //true/false
'&gid=' + tempSheet.getSheetId(); // the sheet's Id
var token = ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url + exportOptions, {
headers: {
Authorization: 'Bearer '+token
}
}).getBlob().setName(tempSpreadsheet.getName()+".pdf");
var pdfFile = DriveApp.createFile(blob);
var downloadLink = HtmlService
.createHtmlOutput('<p>Download your file here.</p>')
.setWidth(200)
.setHeight(100);
SpreadsheetApp.getUi().showModalDialog(downloadLink, "Download PDF");
DriveApp.getFileById(tempSpreadsheet.getId()).setTrashed(true); //Place temporary sheet on trash
}
Then this second link shows you how to download a spreadsheet as PDF to local computer. You can refer best to Tanaike's answer here:
How to download single sheet as PDF (not export to Google Drive)
EDITED (Answer to your question below):
You need to use the sheetID of the second sheet in order to save the PDF for the second sheet.
You should change
FROM:
var sheetId = SpreadsheetApp.getActiveSheet().getSheetId();
TO:
var sheetId = ss.getSheetId();
Because you get the reference of the second spreadsheet by the following logic:
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
So, you just need to use the following code to get the sheet id of the second spreadsheet:
var sheetId = ss.getSheetId();
For your original logic, which is var sheetId = SpreadsheetApp.getActiveSheet().getSheetId(); it will only get the sheetId of the first sheet.
thanks a lot for your hints. I basically reached what I need even if I need a small adaptation.
My spreadsheet has 2 sheets, I would like to add a script button to sheet 1 to download as PDF sheet 2, I slightly modified the script as reported below but I'm still getting my sheet 1 as PDF.
function downloadSheetAsPDF() {
var filename = "Filename.pdf"; // Please set the filename here.
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
var sheetId = SpreadsheetApp.getActiveSheet().getSheetId();
// Creat PDF file as a temporary file and create URL for downloading.
var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + SpreadsheetApp.getActiveSpreadsheet().getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url).getBlob().setName(filename);
var file = DriveApp.createFile(blob);
var dlUrl = "https://drive.google.com/uc?export=download&id=" + file.getId();
// Open a dialog and run Javascript for downloading the file.
var str = '<script>window.location.href="' + dlUrl + '"</script>';
var html = HtmlService.createHtmlOutput(str);
SpreadsheetApp.getUi().showModalDialog(html, "Download PDF");
file.setTrashed(true);
// This is used for closing the dialog.
Utilities.sleep(3000);
var closeHtml = HtmlService.createHtmlOutput("<script>google.script.host.close()</script>");
SpreadsheetApp.getUi().showModalDialog(closeHtml, "Download PDF");
}
Any suggestion?
Thanks.

How can I generate and open pdf as a link with google sheet script?

I'm doing an invoice program in google spreadsheets, and in the program I'm using drawings as buttons (create new invoice, increase/decrease invoice number and clear data). Those functions works well, but I also want to add a button that will create a PDF file and open as a tab in the web browser. So far I had to duplicate the Invoice-sheet as a tempSheet and then remove the buttons, and then download pdf of the tempSheet. I've found this code below which I've tried to change so it will fit with my program (the original program is only for the active sheet). I think maybe the problem is when I use getId() and getSheetId().
If I use ss.getId() in the url I only get the active sheet (invoice-sheet) as the pdf.
If I use tempSheet.getId(), I get an error (ERROR TypeError: tempSheet.getId is not a function)
Do you know what I'm doing wrong? If I understand it correctly it is the same Id for all the sheets and then the different sheets also have its own id which I can get from getSheetId().
getId()- the number after docs.google.com/spreadsheets/d/
getSheetId() - #gid=xxxxxxxxx
function createPDF(){
//create a temporary sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tempSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet('temp');
var tempSheet = SpreadsheetApp.getActive().getSheetByName('temp');
var invoiceNbr = tempSheet.getRange('C3').getValue().toString();
var tempId = tempSheet.getId(); //ERROR TypeError: tempSheet.getId is not a function
var tempSheetId = tempSheet.getSheetId();
var drawings = tempSheet.getDrawings();
for (var i = 0; i < drawings.length; i++) {
drawings[i].remove();
}
const url = 'https://docs.google.com/spreadsheets/d/{ID}/export?'.replace('{ID}', tempId);
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&portrait=true' + // orientation portal, use false for landscape
'&fitw=false' + // fit to page width false, to get the actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid=' + tempSheetId; // the sheet's Id. Change it to your sheet ID.
// You can find the sheet ID in the link bar.
// Select the sheet that you want to print and check the link,
// the gid number of the sheet is on the end of your link.
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var blob = UrlFetchApp.fetch(url + exportOptions, params).getBlob().setName(invoiceNbr+'.pdf');
var pdfFile = DriveApp.createFile(blob);
var downloadLink = HtmlService
.createHtmlOutput('<p>Download your file here.</p>')
.setWidth(200)
.setHeight(100);
SpreadsheetApp.getUi().showModalDialog(downloadLink, "Download PDF");
ss.deleteSheet(tempSheet);
Add SpreadsheetApp.flush() before getSheetByName('temp'):
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet('temp');
SpreadsheetApp.flush();
var tempSheet = SpreadsheetApp.getActive().getSheetByName('temp');
Related
Google Script import form data to new spreadsheet, then send as PDF
Why do we use SpreadsheetApp.flush();?

Google script error: An unexpected error occurred from openById

I' making automation system that google spreadsheet convert it to PDF file and save on my google drive.
I've known this script works successfully on my project page, but doesn't work on trigger.
how could I resolve this problem?
the error message had occurred at 5:39 line.
here
var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();
Please help me!
function savePDFs( optSSId, optSheetId ) {
// If a sheet ID was provided, open that sheet, otherwise assume script is
// sheet-bound, and open the active spreadsheet.
var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();
// Get folder containing spreadsheet, for later export
var parents = DriveApp.getFileById(ss.getId()).getParents();
if (parents.hasNext()) {
var folder = parents.next();
}
else {
folder = DriveApp.getRootFolder();
}
//additional parameters for exporting the sheet as a pdf
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
// Print either the entire Spreadsheet or the specified sheet if optSheetId is provided
+ (optSheetId ? ('&gid=' + sheet.getSheetId()) : ('&id=' + ss.getId()))
// following parameters are optional...
+ '&size=letter' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
var response = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/" + url_ext, options);
var blob = response.getBlob().setName(ss.getName() + '.pdf');
//from here you should be able to use and manipulate the blob to send and email or create a file per usual.
//In this example, I save the pdf to drive
folder.createFile(blob);
}
If the id passed to openById() is not correct(sometimes without reason),
An unexpected error occurred from openById
is thrown, when using SpreadsheetApp.openById().
If the script is bound, you could just use var ss = SpreadsheetApp.getActiveSpreadsheet().
In Google Apps Scripts triggers pass an event object to the function that is being called by them.
If your trigger is calling directly the savePDFs function, then the event object is assigned to optSSId. As this is not a string, this might be the cause of the problem.
Instead of calling savePDFs directly, set your trigger to call another function, then make this fuction call savePDFs without passing any variable. I.E.:
function respondToTimeDrivenTrigger(e){
savePDFs();
}

print spreadsheet as pdf with no margins

I am trying to make things easier for my team so I want to have them print out a given area from my Google Sheets document into a pdf with predefined margins/printing settings. The printed area is not on the active spreadsheet.
I just moved over from Excel to Spreadsheets. I know some VBA but do not really know that much about GAS. I did quite a lot of research but all the samples I found are based on UiApp which not supported any longer.
I found some threads (where I think messages are missing?):
https://plus.google.com/u/1/115432608459317672860/posts/DQTbozyTsKk?cfem=1&pageId=none
Google Apps Script print sheet with margins
The last one I found was this one (PDF margins - Google Script) where I added the settings for margins (does it work that way though? I could not try it out yet because I do not know how to download the pdf. I tried do research but couldn't find anything..
var report = SpreadsheetApp.getActive();
var pdfName = "Angebot";
var sheetName = "Angebot";
var sourceSheet = report.getSheetByName(sheetName);
SpreadsheetApp.getActiveSpreadsheet().toast('Creating the PDF');
// export url
var url = 'https://docs.google.com/spreadsheets/d/'+report.getId()+'/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=A4' // paper size legal / letter / A4
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='+sourceSheet.getSheetId(); // the sheet's Id
+ '&top_margin=0'
+ '&left_margin=0'
+ '&right_margin=0'
+ '&bottom_margin=0'
var token = ScriptApp.getOAuthToken();
// request export url
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var theBlob = response.getBlob().setName(pdfName+'.pdf');
//var attach = {fileName:'Monthly Report.pdf',content:pdf, mimeType:'application/pdf'};
var name = report.getRange("H1:H1").getValues(); // Get Name
var emailTo = report.getRange("H2:H2").getValues(); // Get email
var period = report.getRange("H3:H3").getValues(); // Get Reporting Period
var subject = " - TEST Monthly Report - " + period; // Construct the Subject Line
var message = "Hi " + name + ", here is your latest report for " + period; // email body text
// download pdf
}