Is there any way to include overgrid images in the spreadsheet when downloading as PDF with the script provided by #ZektorH on the question Script to download a range of cells in google sheet as PDF to local computer and other automation scripts? ? I've been trying for a while now with no success.
Script in question:
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
}
The reason why the image was not included in your pdf file is because you did not copy the image available in your source sheet to your temporary sheet. You only copied the cell values in this line tempRange.setValues(range.getDisplayValues());
You can actually export a range to pdf just by adding this option &range=<SheetRange>
Sample Code:
function downloadRangeToPdf() {
var sheet = SpreadsheetApp.getActiveSheet();
var pdfRange = "A1:H50"
//Generate Download As PDF Link
var url = 'https://docs.google.com/spreadsheets/d/{ID}/export?'.replace('{ID}', SpreadsheetApp.getActiveSpreadsheet().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=' + sheet.getSheetId() + // the sheet's Id
'&range='+pdfRange; // Sheet range
var token = ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url + exportOptions, {
headers: {
Authorization: 'Bearer '+token
}
}).getBlob().setName("tempSheetInvoiceExport.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");
}
What it does?
Get the active sheet and define a pdfRange to be exported
Generate an pdf export link with sheet range options '&range='+pdfRange
Create a pdf file from the fetched blob
Display the pdf file url
Output:
Related
I'm using app script to do auto generate PDF in spreadsheet.
But when I execute the code, the result is automatically adding page name on left-top corner and page number on left-down corner.
Is it possible to remove the page name and and page number?
Here is the result:
Here is the code:
function createblobpdf(sheetName, pdfName) {
var sourceSpreadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1IaKnN8GIaLLyNZBMNBKTvml5V1pBzUjXx60BH_bKEdw/edit#gid=1194998923");
var sourceSheet = sourceSpreadsheet.getSheetByName("PAGENAME");
var url = 'https://docs.google.com/spreadsheets/d/' + sourceSpreadsheet.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=true&printtitle=false' // hide optional headers and footers
+ '&pagenum=RIGHT&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM
+ '&gid=' + sourceSheet.getSheetId(); // the sheet's Id
You will need to set the following two parameters:
pagenumbers = false
sheetnames = false
function createblobpdf(sheetName, pdfName) {
var sourceSpreadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1IaKnN8GIaLLyNZBMNBKTvml5V1pBzUjXx60BH_bKEdw/edit#gid=1194998923");
var sourceSheet = sourceSpreadsheet.getSheetByName("PAGENAME");
var url = 'https://docs.google.com/spreadsheets/d/' + sourceSpreadsheet.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
+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM
+ '&gid=' + sourceSheet.getSheetId(); // the sheet's Id
console.log(url);
}
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.
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
}
I have a script that saves my spreadsheet in pdf, but I can't edit the margins and paper size I want.
function Testando() {
var spreadsheet = SpreadsheetApp.getActive();
SpreadsheetApp.flush();
//make pdf
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/'
+ 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX' //the file ID
+ '/export?exportFormat=pdf&format=pdf'
+ '&size=LETTER'
+ '&portrait=true'
+ '&fitw=true'
+ '&top_margin=0.50'
+ '&bottom_margin=0.50'
+ '&left_margin=0.50'
+ '&right_margin=0.50'
+ '&sheetnames=false&printtitle=false'
+ '&pagenum=false'
+ '&gridlines=false'
+ '&fzr=FALSE'
+ '&gid='
+ 'XXXXXXXXXXXXXXXXXXXXXX'; //the sheet's Id
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var fileid = DriveApp.createFile(docurl.getBlob()).setName('Teste.pdf').getId();
var pdf = docurl.getBlob().setName('Teste.pdf');
// var pdf = docurl.getBlob().getAs('application/pdf').setName('testss.pdf');
var filetodel = DriveApp.getFileById(fileid);
DriveApp.getRootFolder().createFolder("Teste"); //comment if folder exists
// if folder exists use next
if (DriveApp.getFoldersByName("Teste").hasNext()){
var folder = DriveApp.getFoldersByName("Teste").next();
filetodel.makeCopy(folder);
}
DriveApp.removeFile(filetodel);
}
I need the PDF to have the custom paper size for → Height: 38 centimeters and Width: 40 centimeters
Page orientation → Landscape
Scale → Fit Width
Margins → Custom Numbers: 0 Left, 0 Right, 0 Start and 0 End.
Could someone help me please?
This is in response to your request:
By the way, would you be able to help me edit the script so that the name of the file saved in PDF equals the value found in Cell "B5" of the "WORK" page of my spreadsheet?
You can fetch the name of the file form the sheet using this line
// Get filename from sheet "Work", cell "B5"
var fileName = spreadsheet.getSheetByName("WORK").getRange("B5").getValue();
And then set the name of the new file by using
.setName(fileName);
There were a few other lines where the code could be optimised. I have added comments to explain. Hope this helps:
function Testando() {
var spreadsheet = SpreadsheetApp.getActive();
SpreadsheetApp.flush();
//make pdf
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' + // Best to place the line break after '+'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXX' + //the file ID
'/export?exportFormat=pdf&format=pdf' +
'&size=LETTER' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0.50' +
'&bottom_margin=0.50' +
'&left_margin=0.50' +
'&right_margin=0.50' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'XXXXXXXXXXXXXXXXXXXXXX'; //the sheet's Id
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
// Get filename from sheet "Work", cell "B5"
var fileName = spreadsheet.getSheetByName("WORK").getRange("B5").getValue();
// Create file from blob and name it
// The newFile is placed in the root folder by default
var newFile = DriveApp.createFile(pdfBlob).setName(fileName);
// if folder exists use next
if (DriveApp.getFoldersByName("Teste").hasNext()){
var folder = DriveApp.getFoldersByName("Teste").next();
// if folder does not exist
} else {
var folder = DriveApp.createFolder("Teste");// new folder created in the root folder by default
}
folder.addFile(newFile); // add new file to folder
DriveApp.removeFile(newFile); // remove file from root folder
}
'&size=AxB' //A = Width you gonna use (inch); B = Height you gonna use (inch)
//Don't use mayus and space. Put both together with the "x" Example: '&size=2.8363636363x5.9055118110' equivalent to 7.2cm and 15cm
here are some examples in how to select the correct margins and paper size.
//FORMATS WITH NO ADDITIONAL OPTIONS
//format=xlsx //excel
//format=ods //Open Document Spreadsheet
//format=zip //html zipped
//CSV,TSV OPTIONS***********
//format=csv // comma seperated values
// tsv // tab seperated values
//gid=sheetId // the sheetID you want to export, The first sheet will be 0. others will have a uniqe ID
// PDF OPTIONS****************
//format=pdf
//size=0,1,2..10 paper size. 0=letter, 1=tabloid, 2=Legal, 3=statement, 4=executive, 5=folio, 6=A3, 7=A4, 8=A5, 9=B4, 10=B5
//fzr=true/false repeat row headers
//portrait=true/false false = landscape
//fitw=true/false fit window or actual size
//gridlines=true/false
//printtitle=true/false
//pagenum=CENTER/UNDEFINED CENTER = show page numbers / UNDEFINED = do not show
//attachment = true/false dunno? Leave this as true
//gid=sheetId Sheet Id if you want a specific sheet. The first sheet will be 0. others will have a uniqe ID.
// Leave this off for all sheets.
// EXPORT RANGE OPTIONS FOR PDF
//need all the below to export a range
//gid=sheetId must be included. The first sheet will be 0. others will have a uniqe ID
//ir=false seems to be always false
//ic=false same as ir
//r1=Start Row number - 1 row 1 would be 0 , row 15 wold be 14
//c1=Start Column number - 1 column 1 would be 0, column 8 would be 7
//r2=End Row number
//c2=End Column number
This being said your var should be something like:
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/'
+ 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX' //the file ID
+ '/export?format=pdf'
+ '&size=0'
+ '&portrait=false'
+ '&fitw=true'
+ '&top_margin=0'
+ '&bottom_margin=0'
+ '&left_margin=0'
+ '&right_margin=0'
+ '&sheetnames=false&printtitle=false'
+ '&pagenum=false'
+ '&gridlines=false'
+ '&fzr=FALSE'
+ '&gid='
+ 'XXXXXXXXXXXXXXXXXXXXXX';
Now the PDF size I don't think that can be changed to a specific size since there are predefined sizes already. I investigated but found nothing helpful, I hope this helps you.
I have converted a temporary google sheet to pdf with exportOptions. However, I want to keep the font size for the text fixed. This causes the text in each row to spill over to the next page. I believe using text wrapping might solve that but I don't know what the code for it is and don't know how to use it in the codes either. I am a novice in programming.
I have tried using commands such as font size, wrap, text-wrap, researched using google and read the documentation for exportOptions, blob, and text sizing as well.
const url =
'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID',
target_ss);
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf / csv / xls / xlsx
'&size=A4' + // paper size legal / letter / A4
'&portrait=false' + // orientation, false for landscape
'&fitw=false' + // fit to page width, false for actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=true' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid='; // the sheet's Id
const token = ScriptApp.getOAuthToken();
const response = UrlFetchApp.fetch(url + exportOptions + target_sheet_Id, {
headers: {
Authorization: 'Bearer ' + token
}});
var blob = response.getBlob().setName(pdfName + '.pdf');
var newFile = folder.createFile(blob);
No error messages but no change in output either probably because those commands don't apply for the exportOptions format.
Iterate through your cells and set the cell wrap of each cell to true using Range.setWrap(true):
var target_ss = "your-spreadsheet-id";
var ss_sheets = SpreadsheetApp.openById(target_ss).getSheets();
for (var i = 0; i < ss_sheets.length; i++){
Logger.log(ss_sheets[i].getDataRange().setWrap(true));
}