Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions converttomarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ function ConvertToMarkdown() {

//Checking if the common folder is present in user's google drive. If not creating it.
if (checkIfFolderExists(commonFolderName)) {
commonFolder = DriveApp.getFolderByName(commonFolderName);
folderIter = DriveApp.getFoldersByName(commonFolderName);
commonFolder = folderIter.next();
} else {
commonFolder = DriveApp.createFolder(commonFolderName);
}
Expand All @@ -182,9 +183,9 @@ function ConvertToMarkdown() {
//Checking if MD file with same name is already present inside that folder. If present it will override the older file. If not it will create the new file.
file = checkIfFileExists(folder, DocumentApp.getActiveDocument().getName() + ".md");
if (file) {
file.replace(text);
file.setContent(text);
} else {
file = DriveApp.createFile(DocumentApp.getActiveDocument().getName() + ".md", text, 'text/plain');
file = folder.createFile(DocumentApp.getActiveDocument().getName() + ".md", text, 'text/plain');
folder.addFile(file);
}

Expand Down Expand Up @@ -257,9 +258,11 @@ function downloadMdFile() {

/* This function checks if there is a folder as given in the parameter exists in Google Drive */
function checkIfFolderExists(folderName) {
var exist = true;
var exist = false;
try {
var testFolder = DriveApp.getFolderByName(folderName);
var testFolder = DriveApp.getFoldersByName(folderName);
if testFolder.hasNext()
exist = testFolder.next()
} catch (err) {
exist = false;
}
Expand Down Expand Up @@ -303,12 +306,12 @@ function checkIfFolderExistsInParent(parentFolder, folderName) {
if (folderCollection.length == 0)
exist = false;
else {
for (var i = 0; i < folderCollection.length; i++) {
if (folderCollection[i].getName() == folderName) {
exist = folderCollection[i];
return exist;
}
}
while (folderCollection.hasNext()) {
childFolder = folderCollection.next()
if (childFolder.getName() == folderName) {
return childFolder
}
}
}
exist = false;
} catch (err) {
Expand All @@ -318,14 +321,19 @@ function checkIfFolderExistsInParent(parentFolder, folderName) {
}
/* This function checks if there is a file as given in the parameter exists in Google Drive */
function checkIfFileExists(folder, fileName) {
var exist = true;
var exist = false;

try {
//var testFolder = DriveApp.getFolder(folderName);
var testFile = folder.find(fileName);
if (testFile.length > 0)
exist = testFile[0];
else
exist = false;
var testFileIter = folder.getFilesByName(fileName);
if (!testFileIter.hasNext()) {
return false
}
var testFile = testFileIter.next()
if (testFile.getSize() > 0) {
exist = testFile;
}
Logger.log("file size not > 0 : " + testFile.getSize());
exist = false;
} catch (err) {
exist = false;
}
Expand Down