Home > Software design >  App Script last row items in multiple variables
App Script last row items in multiple variables

Time:05-24

I try to get the values of the last row separated into multiple variables with app script.

Example:

<div ><table ><thead>
<tr>
<th style="text-align:left">left</th>
<th style="text-align:center">center</th>
<th style="text-align:right">right</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">One</td>
<td style="text-align:center">Two</td>
<td style="text-align:right">Three</td>
</tr>
<tr>
<td style="text-align:left">A</td>
<td style="text-align:center">B</td>
<td style="text-align:right">C</td>
</tr>
</tbody>
</table></div>

Expected output:

Var leftColumn = A;
Var middleColumn = B;
Var rightColumn = C;

I got the values of the last row with the following code:

var data = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues();

CodePudding user response:

I believe your goal is as follows.

  • You want to retrieve the values of last row of the sheet, and want to put the values of columns "A" to "C" in the variables of `leftColumn, middleColumn, rightColumn, respectively.

In your script, how about the following modification?

From:

var data = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues();

To:

var [leftColumn, middleColumn, rightColumn] = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues()[0];
  • By this modification, the variables of leftColumn, middleColumn, rightColumn has the values of columns "A" to "C" of the last row.

Reference:

  • Related