Sébastien
Sébastien

Reputation: 11

How to increment the new data to the last row?

I am trying to create an Apps Script function to update the new data at the last row of a range in Google Sheet. My sheet "import" is currently empty and I have the following error when I add my function ImportPrivacyTagCo() into A1 cell : "Exception: Range not found".

function ImportPrivacyTagCo() {
  
var cellVal= '=IMPORTJSON("myAPIurl")';

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('import');
 var lastrow = sheet.getLastRow() + 1

sheet.getRange(lastrow).setValues([cellVal]);

}

Do you have any idea to fix this error ? Many thanks

Upvotes: 1

Views: 54

Answers (1)

RemcoE33
RemcoE33

Reputation: 1620

Look at how the .getRange() is working.

You need to add the column number. Also it looks like you want to set a formula In that case, use .setFormula() instead.

function ImportPrivacyTagCo() {
  
var cellVal= '=IMPORTJSON("myAPIurl")';

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('import');
 var lastrow = sheet.getLastRow() + 1

sheet.getRange(lastrow, 1).setFormula(cellVal)

}

Upvotes: 1

Related Questions