【问题标题】:Removing Duplicate Rows from All Spreadsheets in a Folder using Google API Scripts使用 Google API 脚本从文件夹中的所有电子表格中删除重复行
【发布时间】:2018-04-25 00:34:55
【问题描述】:

https://developers.google.com/apps-script/articles/removing_duplicates 此链接显示从特定电子表格中删除重复行。我在 goolge 驱动器中有文件夹和子文件夹。我想从文件夹和所有子文件夹中的每个电子表格中删除重复的行。简而言之,将对指定文件夹中的所有电子表格重复链接中的过程。这如何通过循环来完成?

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    要遍历一个文件夹中的文件,您需要执行以下操作。您也可以调整它以包含子文件夹,但目前不包含。

     function listFilesInFolder(folderName) {
      //modified from https://ctrlq.org/code/19854-list-files-in-google-drive-folder
      var folder = DriveApp.getFoldersByName(folderName).next();
      var contents = folder.getFiles();
    
      for (var i = 0; i < contents.length; i++) {
    
        file = contents[i];
    
        if (file.getFileType() == "SPREADSHEET") {
          removeDuplicate(file.getId()); //get the ID and run the remove duplicates script on that particular ss   
        }        
    
      }
    

    };

    你可以把原来的脚本改成这样。

    function removeDuplicates(ssid) {
    var ss = SpreadsheetApp.openById(ssid);//now opening the various ss by ID
     SpreadsheetApp.setActiveSpreadsheet(ss); //and making them the active ss
      var data = sheet.getDataRange().getValues();
      var newData = new Array();
      for(i in data){
        var row = data[i];
        var duplicate = false;
        for(j in newData){
          if(row.join() == newData[j].join()){
            duplicate = true;
          }
        }
        if(!duplicate){
          newData.push(row);
        }
      }
      sheet.clearContents();
      sheet.getRange(1, 1, newData.length, newData[0].length)
          .setValues(newData);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      相关资源
      最近更新 更多