0
votes

I'm using google script in google sheet in order to populate the cell with data that I entered. First I made a form by using html where I store data I entered as object in html (image below).enter image description here

then i call the data I stored in html code with javascript in order to populate the targeted sheet(the last row of the first column)(image below), and it worked.

enter image description here

But then, I created the same form with the same code html but for now I want to populate the last row of the second column. I'm trying using the same code appendRow([null,data.name]), but the problem is it will populate the last row of the sheet. I tried to use getRange then setValues but it doesnt work.

Please help

2

2 Answers

0
votes

Try changing the last row in 'appenData' to

ws.getRange(ws.getLastRow() + 1, 2).setValue(data.name);
0
votes

You can refer to this sample code:

function appendToColumn() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("trial");
  var data = "Testing";
  //Get the column where you want to append the data
  var range = sheet.getRange("B1:B").getValues().flat();
  var row = range.filter(String).length;
  Logger.log(row);
  sheet.getRange(row+1,2).setValue(data);
}

What it does?

  1. Get the range of the column where you want to append your data
  2. Get its value, use array.flat() to change 2-d array to 1-d array
  3. Remove empty cell values using array.filter() and get its length
  4. The length of the array should match the row count of your desired column
  5. Increment the current row count and set its value

Output:

Before:

enter image description here

After:

enter image description here