I have this existing code in google app script that commands my work to convert it to pdf file and will go to its folder.
Example:
I am making invoices, and it would automatically covert to PDF and all PDF files will go to the another folder name "invoices", and all the invoices of the customer will be gather there.
However, I need to separate those pdf files and create folder named on the customers. Meaning, inside the folder invoices, I need to have another folder named accordingly to the name of the customer, and inside the folder of the customer, all of their invoices will gather there.
However, I cannot get the right code for that. What would be the possible code I should add inside my code right now?
function makePDF() {
// Get the currently active spreadsheet URL (link)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var token = ScriptApp.getOAuthToken();
var sheet = ss.getSheetByName("Paycheck");
//Creating an exportable URL
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
var folderID = "1y2_Rw_4l4SZ1i7SaMJLW3953FI9YRTAs"; // Folder id to save in a folder.
var folder = DriveApp.getFolderById(folderID);
var employeeName = ss.getRange("'Paycheck'!C4").getValue()
var pdfName = employeeName " - " Utilities.formatDate(new Date(), "GMT 8", "yyyy-MM-dd");
/* Specify PDF export parameters
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/
var url_ext = '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
'&gid='; // the sheet's Id
// Convert individual worksheet to PDF
var response = UrlFetchApp.fetch(url url_ext sheet.getSheetId(), {
headers: {
'Authorization': 'Bearer ' token
}
});
//convert the response to a blob
var blobs = response.getBlob().setName(pdfName '.pdf');
//saves the file to the specified folder of Google Drive
var newFile = folder.createFile(blobs);
// Define the scope
Logger.log("Storage Space used: " DriveApp.getStorageUsed());
}
CodePudding user response:
I believe your goal is as follows.
- You want to create a new folder with the value of
pdfName
and put the created PDF file in the folder.
In this case, how about modifying your script as follows?
From:
//convert the response to a blob
var blobs = response.getBlob().setName(pdfName '.pdf');
//saves the file to the specified folder of Google Drive
var newFile = folder.createFile(blobs);
To:
//convert the response to a blob
var blobs = response.getBlob().setName(pdfName '.pdf');
// I added below script.
var folders = folder.getFoldersByName(pdfName);
folder = folders.hasNext() ? folders.next() : folder.createFolder(pdfName);
//saves the file to the specified folder of Google Drive
var newFile = folder.createFile(blobs);
- In this modification, if the same folder has already been existing in the folder of
var folder = DriveApp.getFolderById(folderID);
, the file is put to the existing folder. When the same folder has not been existing in the folder, a new folder is created under the folder ofvar folder = DriveApp.getFolderById(folderID);
bypdfName
and the file is put to the created folder.