javascript - 使用发票编号数组创建发票对象,其中初始数组中的每个发票编号都用作 id 属性
问题描述
在谷歌应用脚本中构建脚本。
我从每张发票多行的发票数据表中获取值,以便说明行项目。
到目前为止,我的进展是从列中提取单个发票编号(每个发票编号与单个发票具有的行项目一样多)。
数组 todaysInvoices 如下所示:
[35033817, 35033818, 35033819, 35033820, 35033821]
现在,我需要一种方法来为每个具有不同属性(例如 invoiceDate 和 customerName 等)的发票编号创建一个对象。因此,数组中的初始发票编号应作为“id”属性分配给新发票对象。
我需要帮助才能在 javascript 中使用对象。
如果您需要更多信息,请告诉我。
下面是我的订单表的简化版本的屏幕截图:
这是我的订单的剪报。在显示的列之前和之后,还有更多详细信息,但信息的层次结构已经在图像中
以下是我到目前为止的代码:
const orderSheet = SpreadsheetApp.openById('SPREADSHEETID').getSheetByName('SHEETNAME');
const invoiceTemplate = DriveApp.getFileById('DOCUMENTID');
const tempFolder = DriveApp.getFolderById('FOLDERID');
const invoiceData = orderSheet.getRange(4,7, orderSheet.getLastRow() - 1, 57).getDisplayValues().filter(function (rows){ return rows[0] === 'INVOICED'});
const invDataRepo = SpreadsheetApp.openById('SPREADSHEETID2');
var timestamp = new Date();
function printBulkInvoices() {
logLineItems ();
var todaysInvoices = uniqueInvIDs ();
todaysInvoices.sort();
todaysInvoices.map(String);
//fetchInvData (todaysInvoices);
Logger.log (todaysInvoices)
}
function fetchInvData (invoiceIDs) {
let invoices = {
}
Logger.log(invoices)
invoiceIDs.forEach
}
function fetchLineItems (invoiceDataArray) {
}
// send array of todays unique invoice numbers (later all inv data?) to invdata sheet and log them
function logTodaysInvoices (invIDArr){
invIDArr.forEach
invDataRepo.getSheetByName('invdata').getRange(invDataRepo.getSheetByName('invdata').getLastRow()+1,1,invIDArr.length,1).setValue(invIDArr);
}
// return an array of unique invoice ids from todays invoice data
function uniqueInvIDs (){
let singleArray = invoiceData.map(row => row[5]);
let unique = [...new Set(singleArray)];
return unique;
}
//log incoicedata to invdatarepo-sheet 'lineitems'
function logLineItems (){
invDataRepo.getSheetByName('lineitems').getRange(invDataRepo.getSheetByName('lineitems').getLastRow()+1,2,invoiceData.length,invoiceData[0].length).setValues(invoiceData);
}
解决方案
由于我们看不到您的发票数据表,因此很难准确说出您需要什么。但这里有一些可能会让你开始:
let iobj = {idA:[]};
[35033817, 35033818, 35033819, 35033820, 35033821].forEach((id => {
if(!iobj.hasOwnProperty(id)) {
iobj[id]={date: invoiceDate, name: customName, items:[]};
iobj.idA.push(id);//I find it handy to have an array of object properties to loop through when I wish to reorganize the data after it's all collected
} else {
iobj[id].items.push({item info properties});//I am guessing here that you may wish to addition additional information about the items which are on the current invoice
}
});
要跟进您的问题:
您收集对象数据的循环将开始如下所示:
function getInvoiceData() {
const ss = SpreadsheetApp.getActive();
const ish = ss.getSheetByName('Invoice Data');
const isr = 2;
const hA = ish.getRange(1, 1, 1, ish.getLastColumn()).getValues()[0];
let idx = {};//object return head index into row array based on header title which in this case I assume invoice number is labeled 'Invoicenumber'
hA.forEach((h, i) => {idx[h] = i});
const vs = ish.getRange(isr, 1, ish.getLastRow() - isr + 1, ish.getLastColumn()).getValues();
let iobj = { idA: [] };
vs.forEach(r => {
if (!iobj.hasOwnProperty(r[idx['invoicenumber']])) {
iobj[r[idx['invoicenumber']]] = { date: r[idx['invoicedate']], name: r[idx['customername']], items: [] };
iobj.idA.push(r[idx['invoicenumber']]);
} else {
iobj[r[idx['invoicenumber']]].items.push({ iteminfoproperties:'' });
}
});
}
推荐阅读
- javascript - 离子从组件打开页面或选项卡并运行功能
- c# - C# WPF - 矩形 MouseEnter 和 MouseLeave 事件无法正常工作
- angular - 如何将 stimulsoft js 添加到 webpack angular 5(框架 jhipster)
- python - OpenGL - 一些相交和一些非相交多边形的镶嵌
- tensorflow - tensorflow gpu 仅在 CPU 上运行
- facebook - 没有使用 FB api 在页面列表中获取新的 facebook 页面
- google-cloud-platform - 在 GCP 的编辑后端服务中,“协议”和“命名端口”和“端口号”有什么区别?
- javascript - TypeScript:强制将变量识别为一种类型而不进行强制转换
- linux - Linux,将网页内容另存为文本
- android - 在启动画面中显示 Android 权限