how to wrap the the text in the pdf output file - google-apps-script

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));
}

Related

Fixing TypeError "range.getAs is not a function" with Apps Script's blob [duplicate]

This is my first time posting a question and I'm a complete novice so I apologize upfront if I don't include all the information you may need.
I have a Google Sheet that contains several tabs of data that is hand-keyed into a table. For eg:
Data Entry Table in Sheet
Within each sheet, there are numerous tables where the data from each "company" is entered that falls within a specific region.
What I'm trying to do is adjust a script that will go to the sheet specified, grab a range of cells for a specific table, convert it to a PDF and email it using the email address listed in another tab of the same sheet. I have searched online and found the following script:
function emailSpreadsheetAsPDF() {
DocumentApp.getActiveDocument();
DriveApp.getFiles();
// This is the link to my spreadsheet with the Form responses and the Invoice Template sheets
// Add the link to your spreadsheet here
// or you can just replace the text in the link between "d/" and "/edit"
// In my case is the text: 17I8-QDce0Nug7amrZeYTB3IYbGCGxvUj-XMt8uUUyvI
const ss = SpreadsheetApp.openByUrl("SHEETURLGOESHERE/edit");
// We are going to get the email address from the cell "B7" from the "Invoice" sheet
// Change the reference of the cell or the name of the sheet if it is different
const value = ss.getSheetByName("emails").getRange("A2").getValue();
const email = value.toString();
// Subject of the email message
const subject = ss.getSheetByName("emails").getRange("B2").getValue();
// Email Text. You can add HTML code here - see ctrlq.org/html-mail
const body = "Sent via Generate Invoice from Google Form and print/email it";
// Again, the URL to your spreadsheet but now with "/export" at the end
// Change it to the link of your spreadsheet, but leave the "/export"
const url = 'SHEETURLGOESHERE/export?';
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&landscape=true' + // orientation portal, use false for landscape
'&fitw=true' + // 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=879200050' // the sheet's Id. Change it to your sheet ID.
'&if=false' +
'&ic=false' +
'&r1=51' +
'&c1=0' +
'&r2=102' +
'&c2=20';
// 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()}};
// Generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
// Send the PDF file as an attachement
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: ss.getSheetByName("emails").getRange("B2").getValue() + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});
// Save the PDF to Drive. The name of the PDF is going to be the name of the Company (cell B5)
const nameFile = ss.getSheetByName("01_Allegany_R3").getRange("A52").getValue().toString() +".pdf"
DriveApp.createFile(response.setName(nameFile));
}
While the above code does create the PDF and email it, it's not generating the PDF from the range I enter here:
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&landscape=true' + // orientation portal, use false for landscape
'&fitw=true' + // 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=879200050' // the sheet's Id. Change it to your sheet ID.
'&if=false' +
'&ic=false' +
'&r1=51' +
'&c1=0' +
'&r2=102' +
'&c2=20';
If anyone is able to provide some help to me, I'd greatly appreciate it but I am a novice and not familiar with writing scripts so please be patient, if possible :)
Thank you
You need to put the gid parameter at the end of the export options:
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&landscape=true' + // orientation portal, use false for landscape
'&fitw=true' + // 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
'&if=false' +
'&ic=false' +
'&r1=51' +
'&c1=0' +
'&r2=102' +
'&c2=20'+
'&gid=879200050'; // the sheet's Id. Change it to your sheet ID.

Script download google spreadsheet to pdf - images missing

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:

Google apps script PDF fit to page

I'm using a script to create a PDF-file of a sheet and save it. Everything works great except I can't figure out how to make it "fit whole page". The code below is for "fit to width" (fitw=true) which is described numerous places – but I can't find how to fit to page instead.
function savePdf(spreadsheetId, sheetName, pdfName, pdfFolderId) {
var sheetId = spreadsheetId.getSheetByName(sheetName).getSheetId();
var url_base = spreadsheetId.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf'
+ '&gid=' + sheetId
// following parameters are optional...
+ '&range=D4:AX74'
+ '&size=A4' // paper size: legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&top_margin=0.50'
+ '&bottom_margin=0.50'
+ '&left_margin=0.50'
+ '&right_margin=0.50'
+ '&sheetnames=false' //
+ '&printtitle=false'
+ '&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=true'; // false = do not repeat row headers (frozen rows) on each page
var url_options = {headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),}};
var response = UrlFetchApp.fetch(url_base + url_ext, url_options);
var blob = response.getBlob().getAs('application/pdf').setName(pdfName + '.pdf');
var folder = DriveApp.getFolderById(pdfFolderId);
folder.createFile(blob);
}
I've found a solution!
It's to add
+ '&scale=' + scale
// 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
This should do it:
&horizontal_alignment=CENTER
The fitw parameter should work as expected, but I found a reported issue and I can also experience this problem. Using or not using it does not make any difference.
You can click on the star next to the issue number to receive updates and to give more priority to the bug.

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
}

How to adjust as custom size margins and paper size in script to save google spreadsheet in 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.