简体   繁体   中英

How to update cell in Google App Script after it has been filtered

I have the following scripts which are getting data from the Transactions tab of the sample spreadsheet and then running the logic in sumActual such as grouping and setting values in sumActual tab. This is all working great.

However at the end of the function sumActual where I've noted // set transactions processed. I'd like to set the todays date in column "T" to mark the line processed. However I need to use " const filterProcessedTransactions " from sumActuals because it filters out those rows which have already been processed. I"m not quite sure how to do that.

Any help would be great.

Sample Sheet

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 Math.abs(total + o,Amount) }, 0): startofMonth. startOf } }) // console,log({ result }) // lookup, merge. sum and update sumActuals const obj = result,reduce((o, { category, amount, startofMonth }) => (o[startofMonth + category] = amount, o). {}) console.log({ obj }) 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 })) console.log({ res }) sheet,getRange(3, 2. res,length. res[0].length).setValues(res) // set transactions processed }

 function getTransactions () { const ss = SpreadsheetApp.getActiveSpreadsheet() const transactionSheet = ss.getSheetByName('Transactions') const lastRow = transactionSheet.getLastRow() const lastColumn = transactionSheet.getLastColumn() const values = transactionSheet.getRange(1, 1, lastRow, lastColumn).getValues() const [headers, ...originalData] = values.map(([, b,, d, e,,,,,,,,,,, p, q, r, s, t]) => [b, d, e, p, q, r, s, t]) const res = originalData.map(r => headers.reduce((o, h, j) => Object.assign(o, { [h]: r[j] }), {})) return res }

在此处输入图像描述

Sample Solution:

The simplest way, if you are willing to treat all transactions ran by the script as processed, is to set the column values directly:

This function should do that:

function setTransactionsProcessed () {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const transactionSheet = ss.getSheetByName('Transactions')
  const lastRow = transactionSheet.getLastRow()
  const lastColumn = transactionSheet.getLastColumn()
  const range = transactionSheet.getRange(2,lastColumn,lastRow-1)
  const processedArr = range.getValues();
  for (i = 0; i < processedArr.length; i++) {
    if (processedArr[i] == '') {
      const d = new(Date);
      processedArr[i][0] = d;
    }
  }
  range.setValues(processedArr);
}

And this will be called after filtering out already processed transactions:


  const transactions = getTransactions()
   console.log({ transactions })

  const filterProcessedTransactions = _.filter(transactions, (o) => {
    return !moment(o.Processed).isValid()
  })
   console.log({ filterProcessedTransactions })

  setTransactionsProcessed();

  // Group By Category
  const grouped = _.groupBy(filterProcessedTransactions, (o) => {
    return [o.Category, o.Year, o.Month]
  })
  // console.log({ grouped })

Sample Output:

在此处输入图像描述

在此处输入图像描述

Preserving the original index

You can do it this way and preserve the original index by adding an extra column named idx

function getTransactions () {
  const ss = SpreadsheetApp.getActive()
  const transactionSheet = ss.getSheetByName('Sheet0')
  const lastRow = transactionSheet.getLastRow()
  const lastColumn = transactionSheet.getLastColumn()
  const values = transactionSheet.getRange(1, 1, lastRow, lastColumn).getValues()
  const [headers, ...originalData] = values.map(([, b,, d, e,,,,,,,,,,, p, q, r, s, t]) => [b, d, e, p, q, r, s, t])
  const res = originalData.map((r,i) => headers.reduce((o, h, j) => Object.assign(o, { [h]: r[j],idx:i }), {}))
  return res;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM