【发布时间】:2021-12-21 23:22:56
【问题描述】:
我正在尝试对下面“结果”中的对象执行 forEach,通过“类别”和“startofDate”查找“sumActual”表中的单元格值,然后将两个值相加并设置新的总和值返回“sumActual”中的单元格。
例如,在示例表中,我希望下面的示例表中包含以下内容。
B3:100 (90 + 10)
C3:100(80 + 20)
B4:1600 (1500 + 100)
C4:266(66 + 200)
我只是不确定如何通过“类别”和“startofDate”相交的位置找到单元格值。
提前感谢您的支持。
{ result:
[ { category: 'Credit Card Fees',
month: 'January',
year: 2021,
amount: 90,
startofMonth: '01/01/2021' },
{ category: 'Credit Card Fees',
month: 'February',
year: 2021,
amount: 80,
startofMonth: '02/01/2021' },
{ category: 'Processing',
month: 'January',
year: 2021,
amount: 1500,
startofMonth: '01/01/2021' },
{ category: 'Processing',
month: 'February',
year: 2021,
amount: 66,
startofMonth: '02/01/2021' } ] }
样本表
https://docs.google.com/spreadsheets/d/17SId7mIzO3hVOC36Nq40O0bjPS5YfGOX4wsMU1NlbCU/edit?usp=sharing
function sumActual () {
const _ = LodashGS.load()
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js').getContentText())
const transactions = getTransactions()
// console.log({ transactions })
const filterProcessedTransactions = _.filter(transactions, (o) => {
return !moment(o.Processed).isValid()
})
// console.log({ filterProcessedTransactions })
// Group By Category
const grouped = _.groupBy(filterProcessedTransactions, (o) => {
return [o.Category, o.Year, o.Month]
})
// console.log({ grouped })
// sum amount
const result = _.map(grouped, function (value, key) {
const d = value[0].Month + '/1/' + value[0].Year
const startOf = moment(d).format('MM/DD/YYYY')
const valid = moment(startOf).isValid()
// console.log({ d, startOf, valid })
return {
category: value[0].Category,
month: value[0].Month,
year: value[0].Year,
amount: _.reduce(value, function (total, o) {
return total + o.Amount
}, 0),
startofMonth: startOf
}
})
console.log({ result })
// forEach, index and match where category, month, year match
}
截屏前
【问题讨论】:
-
使用tables 显示您的数据结构。如果您共享电子表格,请注意your email address can be accessed by the public。展示您当前的脚本和研究。
-
相交是什么意思?
-
我必须为我糟糕的英语水平道歉。不幸的是,我无法理解你的问题。例如,关于
B3: 100 (90 + 10),您想将90 + 30放到单元格“B3:100”中吗?而且,当我看到您的示例电子表格时,很遗憾,我找不到startofDate。我为此道歉。那么,为了正确理解您的问题,您能否提供您期望作为图像的示例输入和输出值? -
让我澄清一下。单元格 B3 的值 100 是 B3 (10) 中的当前值加上结果 (90) 中的第一个对象的总和。这个想法是使用结果数据中的“category”和“startofDate”来查找列A类别匹配并且第1行匹配结果中的startofDate的值。截图有帮助吗?
-
感谢您回复并添加更多信息。不幸的是,我不得不为我的理解不佳道歉。我仍然无法理解你的问题。例如,在您的示例输入中,单元格“B4”从
100更改为180。我无法理解这其中的逻辑。能否请教一下实现目标的详细逻辑?
标签: google-apps-script google-sheets