Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I append a blank row in a Google Spreadsheet with Apps Script?

How do I append/insert a blank/empty row in a google spreadsheet using app script?

I've tried:

var ss = SpreadsheetApp.create("test");
var sheet = ss.getActiveSheet();
sheet.appendRow(['Something']);
sheet.appendRow(['']);
sheet.appendRow(['Something Else']);

But this doesn't add a blank row. The following doesn't work either (fails with an error):

sheet.appendRow([]);

A work around is to insert a space. But then this is not a truly blank row.

sheet.appendRow([' ']);

like image 535
User Avatar asked Jan 09 '16 04:01

User


2 Answers

consider:

var nextRow = sheet.getLastRow() + 1;
sheet.getRange('A' + nextRow )
.offset(0, 0, 3)
.setValues([ ['something'], 
             [''], 
             ['something else'] ]);

as it sets your new values all at once

like image 121
Bryan P Avatar answered Oct 22 '22 05:10

Bryan P


Using insertRowsAfter (in conjunction with getMaxRows) seemed to work for me:

var ss = SpreadsheetApp.create("test");
var sheet = ss.getActiveSheet();
sheet.appendRow(['Something']);

// Insert one empty row after the last row in the active sheet.
sheet.insertRowsAfter(sheet.getMaxRows(), 1);

sheet.appendRow(['Something Else']);
like image 21
Cody Avatar answered Oct 22 '22 07:10

Cody