Yas91
Yas91

Reputation: 57

appendRow not putting data into Google spreadsheet

What am i doing wrong here? All it does is create the new file but nothing is in the new spreadsheets.

function summarize_projects() {
  var sheet = SpreadsheetApp
               .openByUrl(theURL).getActiveSheet();

  var rows = sheet.getDataRange(); 
  var numRows = rows.getNumRows(); 
  var values = rows.getValues();
  //var sheet1 = sheet.getRangeByName('GUILLERMO');
  //var sheet2 = sheet.getRangeByName('GLORIA');
  //var sheet3 = sheet.getRangeByName('PV');
  var ssNew = SpreadsheetApp.create("BDATA1");
  Logger.log(rows.getValues);

  var ssNew1 = SpreadsheetApp.create("BDATA2");

  ssNew1.appendRow(values);
  Logger.log(rows.getValue());
  var ssNew2 = SpreadsheetApp.create("BDATA3");

  ssNew2.insertSheet('BDATA3', 'yassir');
  Logger.log(rows.getValue());

Upvotes: 2

Views: 431

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

The method getValues() gets a two dimensional array of data. The appendRow() method can not take a two dimensional array. And you can't use the appendRow() method in this case. You are getting multiple rows of data, and appendRow() can not write multiple rows of data.

You must use this code:

ssNew1.getRange(1,1,values.length, values[0].length).setValues(values);

The above code starts in row one and column one. If you don't want that. If you want the data to be inserted at the end. You will need to use:

ssNew1.getRange(ssNew1.getLastRow() + 1,1,values.length, values[0].length).setValues(values);

But since this is a brand new spreadsheet, there shouldn't be anything in the sheet to begin with.

The syntax for this particular version of getRange() is:

getRange(Starting Row, Starting Column, Number of Rows to Write, Number of Columns to Write)

Upvotes: 1

Related Questions