Egorika Belarus
Egorika Belarus

Reputation: 165

Java Google Sheet API. Append data to specific sheets

I want to append data to a 3 different sheets (using Java and Google Sheet API).

For example, "I like turtles" to the first sheet which sheetId is 1, "123" to the second sheet which sheetId is 2 and any range to the second sheet which sheetId is 3. How to append that data? And more important, how to get sheetId from sheets and set active sheet ? Or any other way to do this. Any suggestions or maybe examples?

Thank you.

Upvotes: 0

Views: 1625

Answers (1)

abielita
abielita

Reputation: 13469

You may refer with this link on how to append data to a specific sheets.

If you simply want to write on a sheet, follow the Basic Writing Sheets guide. You can write on a sheet by using a PUT method:

PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5?valueInputOption=USER_ENTERED

and specify the name of sheet and cell range in like:

{
  "range": "Sheet1!A1:D5",
  "majorDimension": "ROWS",
  "values": [
    ["Item", "Cost", "Stocked", "Ship Date"],
    ["Wheel", "$20.50", "4", "3/1/2016"],
    ["Door", "$15", "2", "3/15/2016"],
    ["Engine", "$100", "1", "30/20/2016"],
    ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
  ],
}

With regard to your spreadsheets.values.append question, I'm sure you've already read what the docs had to say:

Appends values to a spreadsheet. The input range is used to search for existing data and find a "table" within that range. Values will be appended to the next row of the table, starting with the first column of the table.

This is only for adding at the last row of the table.

Also from this page:

Try out the below script and see if that works for you:

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 // This represents ALL the data
 var range = sheet.getDataRange();
 var values = range.getValues();

 // This logs the spreadsheet in CSV format with a trailing comma
 for (var i = 0; i < values.length; i++) {
   var row = "";
   for (var j = 0; j < values[i].length; j++) {
     if (values[i][j]) {  
       row = row + values[i][j];
     }  
     row = row + ",";    
   }    
   Logger.log(row);    
 }

This apps script will help you get data from one spreadsheet to another. Refer to the article getDataRange() for more information on this.

Hope this helps!

Upvotes: 1

Related Questions