Apps 脚本将 google 驱动器中的文件名列表与 google sheet 列中的名称列表进行比较,以将新文件从驱动器添加到 sheet
Apps Script to Compare a list of file names in google drive to a list of names in a google sheet column to add new files from drive to sheet
我有一个应用程序脚本,可以将 google 驱动器文件夹的文件名和 URL 写入指定的 sheet。在其当前形式中,它将驱动器文件夹的全部内容附加到 sheet。我需要它做的是首先将 sheet 上已经存在的文件名与驱动器中的文件名进行比较,然后只附加 sheet 上不存在的文件。
这是我用来比较两个列表并只添加尚不存在的名称的代码。
function listFolderContents() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('files');
const folder = DriveApp.getFolderById('1ShsR9jQAkTCeezouk6sW0NnPpo-kHFjK');
const contents = folder.getFiles();
const existsOnSheet = sheet.getRange(1,1,sheet.getLastRow(),1).getValues();
//console.log(existsOnSheet);
//I want to only append rows with names that do not already exist
var file;
var name;
var link;
var row;
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
if (name === existsOnSheet){
return
}
else{
sheet.appendRow( [name, link] );
}
}
}
也许你应该替换这个:
if (name === existsOnSheet)
有了这个:
if (existsOnSheet.includes(name))
并在此处添加flat()
:
const existsOnSheet = sheet.getRange(1,1,sheet.getLastRow(),1).getValues().flat();
以防万一:
var old_list = ['a', 'b', 'c']
var new_list = ['a', 'b', 'c', 'd', 'e']
var new_files = new_list.filter(x => !old_list.includes(x))
console.log(new_files) // output: ['d', 'e']
不要忘记 Google 驱动器上的文件夹可以包含同名文件。也许使用 ID 或 URL 来识别文件夹中的文件是有意义的。
最终工作脚本:
// replace your folder below with the folderID for which you want a listing
function listFolderContents() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('index');
const folder = DriveApp.getFolderById('1ShsR9jQAkTCeezouk6sW0NnPpo-kHFjK');
const contents = folder.getFiles();
const existsOnSheet = sheet.getRange(2,1,sheet.getLastRow(),1).getValues().flat();
//console.log(existsOnSheet);
//I want to only append rows with names that do not already exist
var file;
var name;
var link;
//var row;
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
if (existsOnSheet.includes(name))
{return
}
else {
sheet.appendRow( [name, link] );
}
}
}```
我有一个应用程序脚本,可以将 google 驱动器文件夹的文件名和 URL 写入指定的 sheet。在其当前形式中,它将驱动器文件夹的全部内容附加到 sheet。我需要它做的是首先将 sheet 上已经存在的文件名与驱动器中的文件名进行比较,然后只附加 sheet 上不存在的文件。
这是我用来比较两个列表并只添加尚不存在的名称的代码。
function listFolderContents() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('files');
const folder = DriveApp.getFolderById('1ShsR9jQAkTCeezouk6sW0NnPpo-kHFjK');
const contents = folder.getFiles();
const existsOnSheet = sheet.getRange(1,1,sheet.getLastRow(),1).getValues();
//console.log(existsOnSheet);
//I want to only append rows with names that do not already exist
var file;
var name;
var link;
var row;
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
if (name === existsOnSheet){
return
}
else{
sheet.appendRow( [name, link] );
}
}
}
也许你应该替换这个:
if (name === existsOnSheet)
有了这个:
if (existsOnSheet.includes(name))
并在此处添加flat()
:
const existsOnSheet = sheet.getRange(1,1,sheet.getLastRow(),1).getValues().flat();
以防万一:
var old_list = ['a', 'b', 'c']
var new_list = ['a', 'b', 'c', 'd', 'e']
var new_files = new_list.filter(x => !old_list.includes(x))
console.log(new_files) // output: ['d', 'e']
不要忘记 Google 驱动器上的文件夹可以包含同名文件。也许使用 ID 或 URL 来识别文件夹中的文件是有意义的。
最终工作脚本:
// replace your folder below with the folderID for which you want a listing
function listFolderContents() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('index');
const folder = DriveApp.getFolderById('1ShsR9jQAkTCeezouk6sW0NnPpo-kHFjK');
const contents = folder.getFiles();
const existsOnSheet = sheet.getRange(2,1,sheet.getLastRow(),1).getValues().flat();
//console.log(existsOnSheet);
//I want to only append rows with names that do not already exist
var file;
var name;
var link;
//var row;
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
if (existsOnSheet.includes(name))
{return
}
else {
sheet.appendRow( [name, link] );
}
}
}```