根据文件名数组搜索文件并检索要记录在 sheet - Google 应用程序脚本中的文件属性
Searching for files based on array of file names and retrieving file properties to be logged in a sheet - Google App Scripts
我一直在尝试解决这个问题,但我是编程新手。我想从一个临时文件夹中获取所有文件名,并根据以前记录到 sheet 的文件名以及其他文件属性(大小、修改日期、ID、url)过滤它们。
在只保留数组中尚未列出的文件名之后,我想从我的临时文件夹中检索具有这些名称的文件并获取它们的文件属性,以便将新行附加到sheet 带有名称、大小、修改日期、id,url)。
这是我目前拥有的:
const invoiceLog = SpreadsheetApp.openById('SPREADSHEETID').getSheetByName('invoicedata');
const invoiceDispatcherLog = SpreadsheetApp.openById('SPREADSHEETID').getSheetByName('invoicedispatcherlog');
const tempInvoiceFolder = DriveApp.getFolderById('TEMPFOLDERID');
const invoiceFolder2021 = DriveApp.getFolderById('INVOICEFOLDERID');
function invoiceLogger () {
// get new Invoices from tempFolder and make an array of their names
let newInvoiceFileNames = [];
let newInvoices = tempInvoiceFolder.getFiles();
while (newInvoices.hasNext()){
file =newInvoices.next();
let row = []
row.push(file.getName())
newInvoiceFileNames.push(row);
Logger.log(row)
}
// filter the array of new invoices by existing invoice names in the invoice log
let newInvoiceFileNamesFlat = newInvoiceFileNames.map(function(row) {return row [0];});
let existingInvoices = invoiceLog.getRange('A2:A').getValues();
let existingInvoicesFlat = existingInvoices.map(function(row) {return row [0];});
var cleanInvoiceNames = newInvoiceFileNamesFlat.filter(item => !existingInvoicesFlat.includes(item))
//let markedFiles = DriveApp.getFilesByName(cleanInvoiceNames);
cleanInvoiceNames.forEach(SearchFiles)
我确定我这里有一些冗余,我还不能完全理解函数和方法如何相互关联的所有方面,但非常感谢您提供一些指导。如果有任何不清楚的地方以及我是否可以提供更多信息,请告诉我。
更新发票数据
function updateInvoiceData() {
const ss = SpreadsheetApp.openById('ssid');
const ish = ss.getSheetByName('invoicedata');
const lnames = ish.getRange(2, 1, ish.getLastRow() - 1).getValues().filter(r => r[0]).flat();//listed names
const ifldr = DriveApp.getFolderById('TEMPFOLDERID');
let info = [];//new names
let ifiles = ifldr.getFiles();
while (ifiles.hasNext()) {
let ifile = ifiles.next();
let n = ifile.getName();
//if not already listed
if(!~lnames.indexOf(n)) {
info.push([n, ifile.getSize(),ifile.getDateCreated(),ifile.getId(),ifile.getUrl()]);
}
}
ish.getRange(ish.getLastRow() + 1,1,info.length,info[0].length).setValues(info);
}
这获取修改日期:
function updateInvoiceData() {
const ss = SpreadsheetApp.openById(gobj.globals.ssid);
const ish = ss.getSheetByName('Sheet0');
const sr = 2;//data start row
const lr = ish.getLastRow();//data last row
let lnames;
if(lr > 1) {
lnames = ish.getRange(2, 1, ish.getLastRow() - 1).getValues().filter(r => r[0]).flat();//listed names
} else {
lnames = [];
}
const ifldr = DriveApp.getFolderById(gobj.globals.testfolderid);
let info = [];//new names
let ifiles = ifldr.getFiles();
while (ifiles.hasNext()) {
let ifile = ifiles.next();
let n = ifile.getName();
//if not already listed
if(!~lnames.indexOf(n)) {
let d = JSON.parse(Drive.Files.get(ifile.getId())).modifiedDate;
info.push([n, ifile.getSize(),d,ifile.getId(),ifile.getUrl()]);
}
}
ish.getRange(lr + 1,1,info.length,info[0].length).setValues(info);
}
您必须启用驱动器 API 版本 2
我一直在尝试解决这个问题,但我是编程新手。我想从一个临时文件夹中获取所有文件名,并根据以前记录到 sheet 的文件名以及其他文件属性(大小、修改日期、ID、url)过滤它们。
在只保留数组中尚未列出的文件名之后,我想从我的临时文件夹中检索具有这些名称的文件并获取它们的文件属性,以便将新行附加到sheet 带有名称、大小、修改日期、id,url)。
这是我目前拥有的:
const invoiceLog = SpreadsheetApp.openById('SPREADSHEETID').getSheetByName('invoicedata');
const invoiceDispatcherLog = SpreadsheetApp.openById('SPREADSHEETID').getSheetByName('invoicedispatcherlog');
const tempInvoiceFolder = DriveApp.getFolderById('TEMPFOLDERID');
const invoiceFolder2021 = DriveApp.getFolderById('INVOICEFOLDERID');
function invoiceLogger () {
// get new Invoices from tempFolder and make an array of their names
let newInvoiceFileNames = [];
let newInvoices = tempInvoiceFolder.getFiles();
while (newInvoices.hasNext()){
file =newInvoices.next();
let row = []
row.push(file.getName())
newInvoiceFileNames.push(row);
Logger.log(row)
}
// filter the array of new invoices by existing invoice names in the invoice log
let newInvoiceFileNamesFlat = newInvoiceFileNames.map(function(row) {return row [0];});
let existingInvoices = invoiceLog.getRange('A2:A').getValues();
let existingInvoicesFlat = existingInvoices.map(function(row) {return row [0];});
var cleanInvoiceNames = newInvoiceFileNamesFlat.filter(item => !existingInvoicesFlat.includes(item))
//let markedFiles = DriveApp.getFilesByName(cleanInvoiceNames);
cleanInvoiceNames.forEach(SearchFiles)
我确定我这里有一些冗余,我还不能完全理解函数和方法如何相互关联的所有方面,但非常感谢您提供一些指导。如果有任何不清楚的地方以及我是否可以提供更多信息,请告诉我。
更新发票数据
function updateInvoiceData() {
const ss = SpreadsheetApp.openById('ssid');
const ish = ss.getSheetByName('invoicedata');
const lnames = ish.getRange(2, 1, ish.getLastRow() - 1).getValues().filter(r => r[0]).flat();//listed names
const ifldr = DriveApp.getFolderById('TEMPFOLDERID');
let info = [];//new names
let ifiles = ifldr.getFiles();
while (ifiles.hasNext()) {
let ifile = ifiles.next();
let n = ifile.getName();
//if not already listed
if(!~lnames.indexOf(n)) {
info.push([n, ifile.getSize(),ifile.getDateCreated(),ifile.getId(),ifile.getUrl()]);
}
}
ish.getRange(ish.getLastRow() + 1,1,info.length,info[0].length).setValues(info);
}
这获取修改日期:
function updateInvoiceData() {
const ss = SpreadsheetApp.openById(gobj.globals.ssid);
const ish = ss.getSheetByName('Sheet0');
const sr = 2;//data start row
const lr = ish.getLastRow();//data last row
let lnames;
if(lr > 1) {
lnames = ish.getRange(2, 1, ish.getLastRow() - 1).getValues().filter(r => r[0]).flat();//listed names
} else {
lnames = [];
}
const ifldr = DriveApp.getFolderById(gobj.globals.testfolderid);
let info = [];//new names
let ifiles = ifldr.getFiles();
while (ifiles.hasNext()) {
let ifile = ifiles.next();
let n = ifile.getName();
//if not already listed
if(!~lnames.indexOf(n)) {
let d = JSON.parse(Drive.Files.get(ifile.getId())).modifiedDate;
info.push([n, ifile.getSize(),d,ifile.getId(),ifile.getUrl()]);
}
}
ish.getRange(lr + 1,1,info.length,info[0].length).setValues(info);
}
您必须启用驱动器 API 版本 2