Last active
April 2, 2021 15:18
-
-
Save barrieroberts/0a0136263dc592f9e0da5e60df0be3ba to your computer and use it in GitHub Desktop.
9-downloadUrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get file 'Reference Doc', make PDF download link & email it. //examples.gs | |
function example91() { | |
var fileId = getFileId(); | |
var downloadUrl = 'https://docs.google.com/document/d/' | |
+ fileId + '/export?format=pdf'; | |
sendEmail(downloadUrl); | |
} | |
//Get file 'Reference Doc', make PDF, add to folder, | |
//get download URL and email it | |
function example92() { | |
var fileInfo = getFileInfo(); | |
var docPdf = makePdf(fileInfo); | |
var downloadUrl = docPdf.getDownloadUrl(); | |
docPdf.addViewer('brgablog1@gmail.com'); | |
sendEmail(downloadUrl); | |
} | |
//commonFunctions.gs - Separate script file | |
//Gets files named "Reference Doc" and return file ID | |
function getFileId() { | |
const folder = DriveApp.getFolderById('ADD YOUR FOLDER ID HERE'); | |
var files = folder.getFilesByName('Reference Doc'); | |
while (files.hasNext()) { | |
var file = files.next(); | |
var fileId = file.getId(); | |
return fileId; | |
} | |
} | |
//Send email with download link | |
function sendEmail(downloadUrl) { | |
MailApp.sendEmail('brgablog1@gmail.com', | |
'Reference Doc - Download Link', | |
'Download the file from here: ' | |
+ downloadUrl); | |
} | |
//Gets files named "Reference Doc" and return file ID | |
function getFileInfo() { | |
const folder = DriveApp.getFolderById('ADD YOUR FOLDER ID HERE'); | |
var files = folder.getFilesByName('Reference Doc'); | |
while (files.hasNext()) { | |
var file = files.next(); | |
var fileId = file.getId(); | |
return { | |
fileId: fileId, | |
file: file, | |
folder: folder | |
} | |
} | |
} | |
//Get file by ID, make PDF, add PDF to folder | |
function makePdf(fileInfo) { | |
const pdf = DriveApp.getFileById(fileInfo.fileId) | |
.getAs('application/pdf'); | |
pdf.setName(fileInfo.file.getName() + ".pdf"); | |
const docPdf = DriveApp.createFile(pdf); | |
docPdf.moveTo(fileInfo.folder); | |
return docPdf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment