将新文件添加到Google云端硬盘时,自动更新Google电子表格

时间:2019-01-16 03:59:28

标签: database google-apps-script google-drive-api new-operator updating

当将新文件添加到某个Google驱动器文件夹时,我需要更新Google电子表格。

它需要提取诸如“名称”,“日期”和“ URL链接”之类的数据。

我了解到Google表格脚本不允许您将“新文件”添加为事件,因此我将不得不每30分钟运行一次,这没有问题; 我无法做的是清除电子表格并创建一个新的电子表格,因为其中内容很多,并且需要大约10分钟的时间才能用Google云端硬盘中的所有数据填充电子表格。

这是我用来创建电子表格的脚本

function listFolders(folder) {
     var sheet = SpreadsheetApp.getActiveSheet();
     sheet.appendRow(["Name", "Sharing Access", "Sharing Permission", "Get Editors", "Get Viewers", "Date", "Size", "URL", "Download", "Description", "Type"]); //writes the headers
     var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");//that long chunk of random numbers/letters in the URL when you navigate to the folder

     var files = folder.getFiles();//initial loop on loose files w/in the folder

     var cnt = 0;
     var file;

     while (files.hasNext()) {
         var file = files.next();
         var listEditors = file.getEditors(); //gets the editor email(s), doesn't show your own as it's assumed
         var editors = [];
         for (var cnt = 0; cnt < listEditors.length; cnt++) {
             editors.push(listEditors[cnt].getEmail());
             Logger.log(editors);
         };
         var listViewers = file.getViewers(); //gets the viewer email(s)
         var viewers = [];
         for (var cnt = 0; cnt < listViewers.length; cnt++) {
             viewers.push(listViewers[cnt].getEmail());
             Logger.log(viewers);
         }
         cnt++;  //data chunk pushes all the file info to the ss

         data = [
             file.getName(),
             file.getSharingAccess(),
             file.getSharingPermission(),
             editors.toString(),
             viewers.toString(),
             file.getDateCreated(),
             file.getSize(),
             file.getUrl(),
             "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
             file.getDescription(),
             file.getMimeType(),
         ];

         sheet.appendRow(data);

     };


     var subfolders = folder.getFolders(); //same thing as above but for all the subfolders in the folder

     while (subfolders.hasNext()) {
         //Logger.log(folder);
         var name = subfolders.next();

         var files = name.getFiles();

         var cnt = 0;
         var file;

         while (files.hasNext()) {
             var file = files.next();
             var listEditors = file.getEditors();
             var editors = [];
             for (var cnt = 0; cnt < listEditors.length; cnt++) {
                 editors.push(listEditors[cnt].getEmail());
                 Logger.log(editors);
             };
             var listViewers = file.getViewers();
             var viewers = [];
             for (var cnt = 0; cnt < listViewers.length; cnt++) {
                 viewers.push(listViewers[cnt].getEmail());
                 Logger.log(viewers);
             }
             cnt++;

             data = [
                 file.getName(),
                 file.getSharingAccess(),
                 file.getSharingPermission(),
                 editors.toString(),
                 viewers.toString(),
                 file.getDateCreated(),
                 file.getSize(),
                 file.getUrl(),
                 "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
                 file.getDescription(),
                 file.getMimeType(),
             ];
             sheet.appendRow(data);
         };
     }
 }

谢谢。

0 个答案:

没有答案