1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+ function convertDriveFolderToMDForDocsAgent ( folderName , outputFolderName = "" ) {
17+ gdoc_count = 0 ;
18+ pdf_count = 0 ;
19+ new_file_count = 0 ;
20+ updated_file_count = 0 ;
21+ unchanged_file_count = 0 ;
22+ gdoc_count , pdf_count , new_file_count , updated_file_count , unchanged_file_count = convertDriveFolder ( folderName , outputFolderName = outputFolderName )
23+ let conversion_count = pdf_count + gdoc_count
24+ let file_count = new_file_count + updated_file_count + unchanged_file_count
25+ Logger . log ( "Converted a total of: " + gdoc_count + " Google Doc files." ) ;
26+ Logger . log ( "Converted a total of: " + pdf_count + " PDF files." ) ;
27+ Logger . log ( "Converted a grand total of: " + conversion_count + " files." ) ;
28+ Logger . log ( "New files: " + new_file_count )
29+ Logger . log ( "Updated a total of: " + updated_file_count + " files." )
30+ Logger . log ( "Files that haven't changed: " + unchanged_file_count ) ;
31+ Logger . log ( "Input directory had a total of: " + file_count + " files." )
32+ }
33+
34+ function convertDriveFolder ( folderName , outputFolderName = "" , indexFile = "" ) {
1635
17- function convertDriveFolderToMDForDocsAgent ( folderName ) {
1836 //Checks if input folder exists or exits
1937 if ( folderExistsInput ( folderName ) ) {
2038 var file_count = 0 ;
2139 var folders = DriveApp . getFoldersByName ( folderName ) ;
22- Logger . log ( "Output directory: " + folderName + "-output" ) ;
23- var folderOutput = folderName + "-output" ;
24- var output_file_name = folderName + "-index" ;
40+ if ( outputFolderName == "" ) {
41+ var folderOutput = folderName + "-output" ;
42+ var output_file_name = folderName + "-index" ;
43+ }
44+ else {
45+ var folderOutput = outputFolderName + "-output" ;
46+ var output_file_name = outputFolderName + "-index" ;
47+ }
48+ Logger . log ( "Output directory: " + folderOutput ) ;
2549 folderExistsOrCreate ( folderOutput ) ;
2650 var folderOutputObj = DriveApp . getFoldersByName ( folderOutput ) ;
2751 if ( folderOutputObj . hasNext ( ) ) {
2852 var folderOutputName = folderOutputObj . next ( ) ;
2953 }
30- var sheet = checkIndexOutputOrCreate ( output_file_name , folderOutputName ) ;
31- var timeZone = Session . getScriptTimeZone ( ) ;
32- var date = Utilities . formatDate ( new Date ( ) , timeZone , "MM-dd-yyyy HH:mm:ss z" ) ;
33- sheet . appendRow ( [ "Created: " , date ] )
34- sheet . appendRow ( [ "Name" , "ID" , "URL" , "Markdown ID" , "Markdown Output" , "Date Created" , "Last Updated" , "Type" , "Folder" , "MD5 hash" , "Status" ] ) ;
54+ if ( indexFile == "" ) {
55+ var sheet = checkIndexOutputOrCreate ( output_file_name , folderOutputName ) ;
56+ var timeZone = Session . getScriptTimeZone ( ) ;
57+ var date = Utilities . formatDate ( new Date ( ) , timeZone , "MM-dd-yyyy HH:mm:ss z" ) ;
58+ sheet . appendRow ( [ "Created: " , date ] )
59+ sheet . appendRow ( [ "Name" , "ID" , "URL" , "Markdown ID" , "Markdown Output" , "Date Created" , "Last Updated" , "Type" , "Folder" , "MD5 hash" , "Status" ] ) ;
60+ }
61+ else {
62+ var sheet = indexFile
63+ }
64+ // var sheet_id = sheet.getId();
3565 var foldersnext = folders . next ( ) ;
3666 var myfiles = foldersnext . getFiles ( ) ;
3767 var new_file_count = 0 ;
@@ -54,6 +84,22 @@ function convertDriveFolderToMDForDocsAgent(folderName) {
5484 else {
5585 var fid = myfile . getId ( ) ;
5686 }
87+ if ( ftype == "application/vnd.google-apps.folder" ) {
88+ var folder = DriveApp . getFolderById ( fid ) ;
89+ Logger . log ( "Sub-directory: " + folder ) ;
90+ sub_gdoc_count = 0 ;
91+ sub_pdf_count = 0 ;
92+ sub_new_file_count = 0 ;
93+ sub_updated_file_count = 0 ;
94+ sub_unchanged_file_count = 0 ;
95+ sub_gdoc_count , sub_pdf_count , sub_new_file_count , sub_updated_file_count , sub_unchanged_file_count = convertDriveFolder ( folder , outputFolderName = foldersnext , indexFile = sheet ) ;
96+ gdoc_count += sub_gdoc_count ;
97+ pdf_count += sub_pdf_count ;
98+ new_file_count += sub_new_file_count ;
99+ updated_file_count += sub_updated_file_count ;
100+ unchanged_file_count += sub_unchanged_file_count ;
101+ continue ;
102+ }
57103 var fname = sanitizeFileName ( myfile . getName ( ) ) ;
58104 var fdate = myfile . getLastUpdated ( ) ;
59105 var furl = myfile . getUrl ( ) ;
@@ -158,7 +204,7 @@ function convertDriveFolderToMDForDocsAgent(folderName) {
158204 var saved_file_id = saved_file . getId ( ) ;
159205 Logger . log ( "Finished converting file: " + fname + " to markdown." ) ;
160206 Logger . log ( "Markdown file: " + saved_file ) ;
161- Logger . log ( "Clearing temporary gdoc: " ) ;
207+ Logger . log ( "Clearing temporary gdoc" ) ;
162208 let output_file = DriveApp . getFileById ( output_id ) ;
163209 output_file . setTrashed ( true ) ;
164210 status = "New content" ;
@@ -182,19 +228,13 @@ function convertDriveFolderToMDForDocsAgent(folderName) {
182228 hash_str ,
183229 status ,
184230 ] ;
185- row_number = file_count + start_data_row ;
186231 sheet . appendRow ( metadata ) ;
232+ // Return final row to inserRichText into correct rows
233+ row_number = sheet . getLastRow ( ) ;
187234 insertRichText ( sheet , original_chip , "C" , row_number ) ;
188235 insertRichText ( sheet , md_chip , "E" , row_number ) ;
189236 insertRichText ( sheet , folder_chip , "I" , row_number ) ;
190237 }
191238 }
192- let conversion_count = pdf_count + gdoc_count
193- Logger . log ( "Converted a total of: " + gdoc_count + " Google Doc files." ) ;
194- Logger . log ( "Converted a total of: " + pdf_count + " PDF files." ) ;
195- Logger . log ( "Converted a grand total of: " + conversion_count + " files." ) ;
196- Logger . log ( "New files: " + new_file_count )
197- Logger . log ( "Updated a total of: " + updated_file_count + " files." )
198- Logger . log ( "Files that haven't changed: " + unchanged_file_count ) ;
199- Logger . log ( "Input directory had a total of: " + file_count + " files." )
239+ return gdoc_count , pdf_count , new_file_count , updated_file_count , unchanged_file_count
200240}
0 commit comments