forked from clearf/gdocs2md
-
Notifications
You must be signed in to change notification settings - Fork 0
exportRmd::convertFolder
Louis edited this page Mar 13, 2015
·
2 revisions
The convertFolder
function is called to convert all documents within the folder with ID set to the 'folder_id' script property value within setupScript
, and uses this document's parent folder to locate the Rmarkdown output folder. Note that it's just the parent folder of the active document. It's possible to set this ID manually, see the commented out portion of setupScript
.
The main document processing work is done by convertDocToRmd.
function convertFolder() {
var scriptProperties = PropertiesService.getScriptProperties();
var folder_id=scriptProperties.getProperty("folder_id");
var source_folder = DriveApp.getFolderById(folder_id);
var Rmarkdown_folders = source_folder.getFoldersByName("Rmarkdown");
var Rmarkdown_folder;
if (Rmarkdown_folders.hasNext()) {
Rmarkdown_folder = Rmarkdown_folders.next();
} else {
// Create a Rmarkdown folder if it doesn't exist.
Rmarkdown_folder = source_folder.createFolder("Rmarkdown");
}
// Only try to convert google docs files.
var gdoc_files = source_folder.getFilesByType("application/vnd.google-apps.document");
// For every file in this directory
while(gdoc_files.hasNext()) {
var gdoc_file = gdoc_files.next()
var filename = gdoc_file.getName();
var Rmd_files = Rmarkdown_folder.getFilesByName(filename + ".Rmd");
var update_file = false
if (Rmd_files.hasNext()) {
var Rmd_file = Rmd_files.next();
if (Rmd_files.hasNext()){ // There are multiple Rmarkdown files; delete and rerun
update_file = true;
} else if (Rmd_file.getLastUpdated() < gdoc_file.getLastUpdated()) {
update_file = true;
}
} else {
// There is no folder and the conversion needs to be rerun
update_file = true;
}
if (update_file) {
convertDocToRmd(DocumentApp.openById(gdoc_file.getId()), Rmarkdown_folder);
}
}
}