【问题标题】:Update Cell using column and row using app script使用应用脚本使用列和行更新单元格
【发布时间】: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


【解决方案1】:

我相信你的目标如下。

  • 使用您的问题中的值,您希望使用 Google Apps 脚本从上图转换为下图。

在这种情况下,下面的示例脚本怎么样?

示例脚本:

function myFunction() {
  // This value is from your question.
  const { result } = {
    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'
      }]
  };

  const obj = result.reduce((o, { category, amount, startofMonth }) => (o[startofMonth + category] = amount, o), {});
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sumActual");
  const [[, ...header], , ...values] = sheet.getDataRange().getValues();
  const res = values.map(([h, ...v]) => v.map((f, j) => {
    const key = Utilities.formatDate(header[j], Session.getScriptTimeZone(), "MM/dd/yyyy") + h;
    return obj[key] ? obj[key] + f : f;
  }));
  sheet.getRange(3, 2, res.length, res[0].length).setValues(res);
}
  • 运行此脚本时,工作表“sumActual”的单元格将使用您问题中的值进行更新。您问题中底部图像中的目标可以通过上图实现。

注意:

  • 此示例脚本适用于您的示例图像。所以当你的实际情况与它不同时,这个脚本可能无法使用。请注意这一点。

参考资料:

【讨论】:

  • 这很棒。我将继续使用它,并试图更好地了解它是如何工作的。非常感谢你,再次为我之前的错误感到抱歉。
  • @JAK 感谢您的回复。没问题。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
相关资源
最近更新 更多