Question

I am trying to come up with a script that will simply capture the value of a cell and paste that value in another cell. The cell value it is capturing is a simple, unformatted cell containing the number 32. My code below works but the result is not 32 in the target cell. Instead, the result is [[32.0]]. Can someone help me understand what it is adding the brackets and the .0?

function test(row) { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();// Define active sheet
  var amountdue = sheet.getRange("A30").getValues();// Capture amount due
  sheet.getRange("B30").setValue(amountdue);// Paste amount due
}
Was it helpful?

Solution

The function that you are using "getValues()" is intended to capture more than one cell. As this function is designed to capture a table it give in output a result that is not the value of one cell but the value of several cells. to display them we need to store them in something that it's easily accessible and structured : an array.
The array is represented as: [cell1Value, cell2Value, cell3Value, ...] .
As there may have more than one row we store each row in an other array: [row1, row2, row3, ...]. so in the end it's something like that:
[ [row1Cell1, row1Cell2, ...], [row2Cell1, row2Cell2, ...] ]

You don't want these brackets, change "getValues()" for "getValue()" without "s".
Or when you set the value use:
amoutdue[0][0]
the first [0] is to say I want the first row (we start at 0) and the second [0]... you already guessed is for the first cell.

I suppose that you are getting the ".0" after "32" because as you are trying to set the array of array in a cell. Javascript don't know how to handle that and try to transform the array of array in full text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top