我一直在嘗試解決這個問題,但我是編程新手。我想從臨時檔案夾中獲取所有檔案名,并根據以前與其他檔案屬性(大小、修改日期、id、url)一起記錄到作業表的檔案名過濾它們。
在只保留陣列中尚未列出的檔案名后,我想從我的臨時檔案夾中檢索具有這些名稱的檔案并獲取它們的檔案屬性,以便將新行附加到具有該名稱的作業表中,大小、修改日期、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)
我確信我在這里有一些冗余,并且我還不能完全理解函式和方法如何互鎖的所有方面,但會非常感謝一些指導。如果有任何不清楚的地方,請告訴我,我是否可以提供更多資訊。
uj5u.com熱心網友回復:
更新發票資料
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);
}
您必須啟用 Drive API 版本 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/352321.html
標籤:javascript 谷歌应用程序脚本 谷歌表格 谷歌驱动器api
