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