I'm trying to create a custom script in Google Sheets that creates text files in the Google Drive.
The Sheet Name will be the folder name, the value in Column A will be the text file name and the value in Column B will be the text inserted into the text file.
I've had success creating it, but I'm having issues expanding it to create a new file for every row and to do multiple sheets.
How can I adjust the code to create additional folders and files when I add a new sheet or a new row?
Here is what I have so far:
function export() {
var sheet = SpreadsheetApp.getActiveSheet();
var filename = sheet.getRange(1, 1).getValue();
var foldername = sheet.getSheetName();
Logger.log('Filename = ' + filename + ' Foldername = ' + foldername);
var pFIT = DriveApp.getRootFolder().getFoldersByName(foldername);
var parentFolder, File ;
if (pFIT.hasNext())
parentFolder= pFIT.next();
if (parentFolder==null) {
parentFolder = DriveApp.getRootFolder().createFolder(foldername);
}
var existing = parentFolder.getFilesByName(filename);
if (existing.hasNext()) {
var file = existing.next();
if (file.getName() === filename) {
file.setTrashed(true);
}
}
var values = sheet.getRange(1, 2).getValues();
var text = values.map(function (a) {return a.join('\t');}).join('\r\n');
parentFolder.createFile(filename, text, MimeType.PLAIN_TEXT);
}
Here is what I'm trying to accomplish when I run the script:
Screenshot 1
Screenshot 2
Screenshot 3