2

In Google Sheets, I'm trying to return the absolute values of a cell, using app script. So far I have the following code:

var SEARCH_COL_IDX = 1; // variable that indicates which column to search, 0 = column A
function SearchInventario() {

var ss      = SpreadsheetApp.getActiveSpreadsheet();
var formS   = ss.getSheetByName("EntryFrmMovimiento"); //Form sheet that captures data

var str     = formS.getRange("E4").getValue(); //cell that contains search criteria
var values= ss.getSheetByName("DataInventario").getDataRange().getValues();//Tab with dataset
for (var i = 0; i < values.length; i++) {
    var row = values[i];
    if (row[SEARCH_COL_IDX] == str) {

    formS.getRange("C10").setValue(row[0]);
    formS.getRange("C11").setValue(row[1]);//This is the cell for which I need Absolute value
   }}}

It returns the data in the dataset fine, but I need cell C11 to then be converted to absolute value in this tab but not in the original dataset.

Any hits, tips or ideas will be greatly appreciated!

Riggs
  • 23
  • 6

2 Answers2

2

Just for educatment purposes. You can make your own Math.abs() FUNction anytime:

formS.getRange("C11").setValue( row[1] < 0 ? -row[1] : row[1] );

const abs = x => x < 0 ? -x : x;

console.log(abs(-25)) // = 25
console.log(abs(5))   // = 5
console.log(abs(0))   // = 0

Or even more funny:

const abs = x => Number(x.toString().replace('-',''))

console.log(abs(-25))   // = 25
console.log(abs(-1.25)) // = 1.25
console.log(abs(5))     // = 5
console.log(abs(0))     // = 0
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
1

Try this:

formS.getRange("C11").setValue(Math.abs(row[1]));

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
Kris
  • 537
  • 2
  • 9