Skip to content

Commit

Permalink
Support deleting folder content or cancel convert
Browse files Browse the repository at this point in the history
Why:

 * Currently when generating we just add files to folder causing
   duplicates. Better if we could keep the folder clean or overwrite files
   instead of creating duplicates.

This change addreses the need by:

 * Offer to delete the folder if already found or cancel if you do not want
   to delete.
  • Loading branch information
maxandersen committed Jul 17, 2016
1 parent c27c171 commit f2f42e1
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions converttoasciidoc.gapps
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var ADOC_MIMETYPE = "text/asciidoc";

// Open handler to add Menu
function onOpen(e) {
var ui = DocumentApp.getUi();
Expand Down Expand Up @@ -50,7 +52,7 @@ function ConvertToAsciiDocEmail() {

// Add asciidoc document to attachments
convertedDoc.attachments.push({"fileName":DocumentApp.getActiveDocument().getName()+".adoc",
"mimeType": "text/plain", "content": convertedDoc.text});
"mimeType": ADOC_MIMETYPE, "content": convertedDoc.text});

// In some cases user email is not accessible
var mail = Session.getActiveUser().getEmail();
Expand Down Expand Up @@ -107,7 +109,21 @@ function ConvertToAsciiDocFile() {
found = parent.createFolder(targetDirName);
} else {
Logger.log("Reusing existing target");
} // TODO: inform user if we create or overwrite ?
var ui = DocumentApp.getUi();
var result = ui.alert('Existing target folder found!', 'You already have "' + targetDirName + '" in your Drive. Delete folder or Cancel ?',
ui.ButtonSet.OK_CANCEL)

if(result == ui.Button.OK) {
Logger.log("Trashing target folder...");
found.setTrashed(true);
Logger.log("Re-Creating output folder...");
found = parent.createFolder(targetDirName);

} else {
Logger.log("Do not delete target folder, stopping!");
return;
}
}

// Write all files to target folder
for(var file in convertedDoc.files) {
Expand All @@ -121,7 +137,7 @@ function ConvertToAsciiDocFile() {
}

// Write mardown file to target folder
found.createFile(DocumentApp.getActiveDocument().getName() + ".adoc", convertedDoc.text, "text/plain");
found.createFile(DocumentApp.getActiveDocument().getName() + ".adoc", convertedDoc.text, ADOC_MIMETYPE);
}

function processSection(section) {
Expand Down

0 comments on commit f2f42e1

Please sign in to comment.