LanceS
LanceS

Reputation: 87

How to paste a row of data from one google sheet to another sheet in a different workbook

After running this code:

function SendData() {
  var sss = SpreadsheetApp.openByUrl("URL"); // sss = source spreadsheet
  var ss = sss.getSheetByName('Verify'); // ss = source sheet
  var rangeToCopy = ss.getRange("B28:Q28");
  var copiedData = rangeToCopy.getValues();
  var tss = SpreadsheetApp.openByUrl("URL"); // tss = target spreadsheet
  var ts = tss.getSheetByName('log'); // ts = target sheet
  ts.appendRow(copiedData);
   
  
}  

The script writes to the target sheet a random value that looks like this, in cell A1:

[Ljava.lang.Object;@70cb5117

What am I missing?

Upvotes: 1

Views: 85

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

The getValues() method gets a 2D array. Each inner array represents one row. Each element of each inner array represents one cell. The appendRow() method takes a 1D array. Because your range is only one row of data, then you could convert the 2D array to a 1D array, and use appendRow()

copiedData = copiedData.toString().split(",");//convert 2D to 1D
ts.appendRow(copiedData);

If you were getting more than 1 row of data, then you could not use appendRow(). You would need to use getRange(startRow,startColumn, numberOfRows, numberOfColumns).setValues(2D_Array)

It would look like this:

var lastRow = ts.getLastRow();
ts.getRange(lastRow+1,1,copiedData.length,copiedData[0].length).setValues(copiedData);

Upvotes: 1

Related Questions