Is there a way to save a Google Slides as PDF file by using Google Apps Script?
I could only find solutions for saving Google Docs and Sheets.
How about this sample script? When Google Docs (Spreadsheet, Document and Slide) is saved and/or downloaded using DriveApp.createFile(), the file format automatically becomes PDF. So we can use the following script.
Sample script :
var blob = DriveApp.getFileById("### Slide file ID ###").getBlob();
DriveApp.createFile(blob);
Note :
In this case, the filename of created PDF file is the same to the slide.
Edit :
If you want to copy the slide file, please use a following script.
var file = DriveApp.getFileById("### Slide file ID ###");
file.makeCopy(DriveApp.getFolderById("### Destination folder ID ###"));
This could work by replacing DocumentApp with SlideApp
Beware it also deletes a previously created pdf of the same name if it exists in the drive folder.
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Save PDF copy')
.addItem('Save PDF', 'SavePDF')
.addToUi();
}
function SavePDF() {
var theDoc = DocumentApp.getActiveDocument();
var id = DocumentApp.getActiveDocument().getId()
var ui = DocumentApp.getUi();
var folderString = DriveApp.getFileById(id).getParents().next().getName();
var folder = DriveApp.getFileById(id).getParents().next();
var theBlob = theDoc.getAs('application/pdf')
var thePDFName = theBlob.getName()
var theFiles = DriveApp.getFilesByName(theBlob.getName())
while (theFiles.hasNext()) {
var theFile = theFiles.next()
if (theFile.getParents().next().getName() == folderString) {
theFile.setTrashed(true)
}
}
var newFile = folder.createFile(theBlob);
ui.alert('Saved to ' + folderString + " " + theBlob.getName())
}
let pdf = DriveApp.getFileById(copySlide.getId())
.getBlob()
.getAs("application/pdf");
pdf.setName("filename.pdf");
let file = DriveApp.getFolderById(FOLDER_ID).createFile(pdf);
Related
excuse my bad English.
I have a main Google Sheet that export that from other sheets to fill the information. This action depends on the number in a cell, then I would download the PDF of the sheet. But the name would automatically be the sheet's name. Is there a script I could use so that the name of the pdf file would be one of the data on a specific cell?
I would gladly appreciate the help. This is so I could get this done more quickly since there are so many people.
I tried to follow some code from another thread: [https://stackoverflow.com/questions/56215898/how-to-download-single-sheet-as-pdf-not-export-to-google-drive]
And ended up with something like this:
function onOpen() {
var submenu = [{name:"Save PDF", functionName:"generatePdf"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Export', submenu);
}
function generatePdf() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = ss.getActiveSheet().getSheetId();
var valor = SpreadsheetApp.getActiveSheet().getRange('G2').getValue();
var filename = valor;
// Creat PDF file as a temporary file and create URL for downloading.
var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.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, "sample");
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, "sample");
}
Didn't work, just maybe one time after so many tries.
Then I tried this: https://gist.github.com/primaryobjects/6370689c6f5fd3799ea53f89551eced7
But the PDF that exports have the cell's lines in the background... So, it doesn't look good.
Is there a way for the export to use the pdf download method, so it looks clean, but automatically put the name of the pdf from a cell value?
need some help and hope you can help me :)
I have a google spreadsheet document and need to do some actions per script:
Sheet: "Sheet1"
Range: "A1:J39"
print out with settings (landscape, perfect width)
save as PDF document in a folder in a shared google drive (same settings like no 1)
send PDF file per mail to adresses which listed in an other sheet
hope you can help me with this problem....
thx
I'm giving below code I use to send a full sheet as PDF.
You can modify it slightly to
1.Hide unwanted rows and columns
2.Include PDF export options
function send_sheet(){
var today=new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ltrsht = ss.getSheetByName("Letter");
var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();
for(var i =0;i<sheets.length;i++){
if(sheets[i].getName()!="Letter"){ sheets[i].hideSheet() }
}
var pdf = DriveApp.getFileById(ss.getId());
var theBlob = pdf.getBlob().getAs('application/pdf').setName(ltrsht.getRange("C16").getValue()+".pdf");
var folderID = ""; // Folder id to save in a folder
var folder = DriveApp.getFolderById(folderID);
var newFile = folder.createFile(theBlob);
var body = 'Dear ' + ltrsht.getRange("C16").getValue() +',\n\nPL. find your ' + ltrsht.getRange("C11").getValue() +' enclosed.\n\nHRD Megawin Switchgear';
GmailApp.sendEmail(ltrsht.getRange("E17").getValue(), ltrsht.getRange("C11").getValue() + " from Megawin HRD", body, {attachments: [theBlob]});
var empsht = ss.getSheetByName("Emp");
empsht.showSheet();
ltrsht.hideSheet();
}
First you have to hide all sheets other than the target sheet
Hide unwanted rows and columns
Convert to PDF
Save in folder
Send to email id which is stored some cell
See below how to format the PDF
https://support.google.com/docs/thread/3457043?hl=en
Background: I have about 1500 Google Docs in a Google Services account shared directory. Some of those docs have hyperlinks. I need to replace the URL in hyperlinks with new URLs using a Google Script.
I found this script here. The script below will successfully replace URL's within the body of any Google Doc in my drive, but it will not replace any URL's within hyperlinks.
How can I modify this script to replace the URL within a hyperlink instead of just the body text?
var files = DriveApp.getFiles(); // Note: this gets *every* file in your Google Drive
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
doc.replaceText("http://www.googledoclink1.com", "http://www.googledoclinkA.com");
doc.replaceText("http://www.googledoclink2.com", "http://www.googledoclinkB.com");// Note: This will be repeated probably 500 times
}
Logger.log("Done")
}
You need to replace both the text and the hyperlink separately
The hyperlink can be modified with setLinkUrl().
Modify your code in a following way to make it work:
function myFunction() {
var oldLink="http://www.googledoclink1.com";
var newLink="http://www.googledoclinkA.com";
var oldLink2="http://www.googledoclink2.com";
var newLink2="http://www.googledoclinkB.com";
var files = DriveApp.getFiles(); // Note: this gets *every* file in your Google Drive
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
var link=doc.getBody().findText(oldLink).getElement().asText();
var link2=doc.getBody().findText(oldLink2).getElement().asText();
link.setLinkUrl(newLink);
doc.replaceText(oldLink, newLink);
link2.setLinkUrl(newLink2);
doc.replaceText(oldLink2, newLink2);
}
Logger.log("Done")
}
I wrote a Google Apps script in a Spreadsheet that convert the Spreadsheet to pdf. This works.
How can I copy this generated pdf file automatically using the same script to PC's local folder (Desktop f.e.)
Code.gs
function onOpen() {
var submenu = [{name:"Save PDF", functionName:"generatePdf"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Export', submenu);
}
function generatePdf() {
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var pdfName = Browser.inputBox("Name of generated pdf");
var save2folder = DriveApp.getRootFolder();
var theBlob = sourceSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var pdfFile = save2folder.createFile(theBlob);
}
Thanks
I have tried to use "UrlFetchApp.Fetch" in google apps script to retrieve data from https://metoc.ndbc.noaa.gov/web/guest/jtwc".
However, the information highlighted in red as shown here "web capture" cannot be captured in the file I downloaded. Please help, thanks.
var FILE_NAME = 'data.txt'
var Google_DRive_ID = 'your google drive folder id'
var RESOURCE_URL = 'https://metoc.ndbc.noaa.gov/web/guest/jtwc'
var folder = DriveApp.getFolderById(Google_DRive_ID);
var exportUrl = RESOURCE_URL
var data = UrlFetchApp.fetch(exportUrl)
folder.createFile(FILE_NAME, data)
When I saw the source of https://metoc.ndbc.noaa.gov/web/guest/jtwc, it was found that the source you want is included in iframe with the id="_48_INSTANCE_0SiamlX2KcM6_iframe". The source URL is src="/ProductFeeds-portlet/img/jtwc/html/coop.jsp?". When your script is modified using the URL, it is as follows.
Modified script :
var root = "https://metoc.ndbc.noaa.gov";
var src = "/ProductFeeds-portlet/img/jtwc/html/coop.jsp?"; // You can also use "/ProductFeeds-portlet/img/jtwc/html/coop.jsp"
var FILE_NAME = 'data.txt'
var Google_DRive_ID = 'your google drive folder id'
var RESOURCE_URL = root + src;
var folder = DriveApp.getFolderById(Google_DRive_ID);
var exportUrl = RESOURCE_URL
var data = UrlFetchApp.fetch(exportUrl)
folder.createFile(FILE_NAME, data)
If I misunderstand your question, I'm sorry.