1

So I would like to map a resulting value to each row within a range of values, populate the results into a new array and paste the created array to enrich the source data.

So the callback function in the map method should check if the amount is < > 0 and return revenue expense respectively. I'm struggling in defining the map method and callback function. How can I "access" the amount column? And do I need one or two parameters for the arrow function?

function example_function() {

  // Get active Sheet
  sheet = SpreadsheetApp.getActiveSheet()

  // Get the range
  range = sheet.getDataRange()

  // Get its values
  values = range.getValues()

  // Create new array by mapping the desired results
  resulting_array = values.map((a, b) => {

    // "if value in current row and column b is > 0 then return "revenue", else "expense"
    // what would be the app script language equivalent to this condition?


  });

  // Paste the created array
  sheet.getRange(2, 3).setValues(resulting_array);
}

Source

Item Amount P&L
Shopify -30
Paypal 40

Desired Outcome

Item Amount P&L
Shopify -30 Expense
Paypal 40 Revenue
JSRB
  • 2,492
  • 1
  • 17
  • 48
  • Related:https://stackoverflow.com/questions/63720612/what-does-the-range-method-getvalues-return-and-setvalues-accept – TheMaster Feb 06 '22 at 10:19

1 Answers1

1

It can be done many ways. I'd try this:

function example_function() {

    // Get active Sheet
    sheet = SpreadsheetApp.getActiveSheet()
  
    // Get the range
    range = sheet.getDataRange()
  
    // Get its values
    values = range.getValues()
  
    // Create new array by mapping the desired results
    resulting_array = values.map(row => row[1] > 0 ? ['Revenue'] : ['Expense']);
  
    // Paste the created array
    sheet.getRange(1,3,resulting_array.length,1).setValues(resulting_array);
}
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23