google spreadsheet feeds with multiple worksheets - google-sheets-api

How to get feeds from multiple sheets using google spreadsheet feeds api?
Below URL fetches the feeds from first sheet only. In my spreadsheet I have 3 work sheets and I want to fetch remaining sheets data as well.
https://spreadsheets.google.com/feeds/list/XXXXMYKEYXXXX/od6/public/values?alt=json-in-script
How to fetch them?
I tried below without success:
https://spreadsheets.google.com/feeds/list/XXXXMYKEYXXXX/od7/public/values?alt=json-in-script
Note od7 instead of od6
UPDATE
URL for the spreadsheet feed is
https://spreadsheets.google.com/feeds/list/1c53H0piyduOV6zRwS54A7MDZ3SooJOmj4O52Xd0dyIA/od6/public/values?alt=json-in-script
In the spreadsheet I have TopicA,TopicB,TopicC sheets. Feeds response contains TopicA information only.

It seems worksheet IDs are numbered from 1 to n and not like od6, od7.
Using below urls, I was able to get individual worksheet's data
https://spreadsheets.google.com/feeds/list/1c53H0piyduOV6zRwS54A7MDZ3SooJOmj4O52Xd0dyIA/1/public/values?alt=json fetches first worksheet
https://spreadsheets.google.com/feeds/list/1c53H0piyduOV6zRwS54A7MDZ3SooJOmj4O52Xd0dyIA/2/public/values?alt=json 2nd sheet
https://spreadsheets.google.com/feeds/list/1c53H0piyduOV6zRwS54A7MDZ3SooJOmj4O52Xd0dyIA/3/public/values?alt=json 3rd sheet and so on
Note the /1, /2 and /3 after the key (1c53H0piyduOV6zRwS54A7MDZ3SooJOmj4O52Xd0dyIA) in the url

Per the Google Spreadsheets API:
To determine the URL of a given spreadsheet's worksheets feed, find
that spreadsheet's entry in the spreadsheets feed, as described in the
previous section. Then examine the link element that has
rel="http://schemas.google.com/spreadsheets/2006#tablesfeed". That
element's href value provides the URL for that spreadsheet's
worksheets feed.
To request a list of worksheets in a given spreadsheet, issue a GET
request to that URL, with an appropriate authorization header:
GET https://spreadsheets.google.com/feeds/worksheets/key/private/full
The returned result contains a URL for each worksheet in that spreadsheet, which you can then query to pull information from each worksheet you want to get data from. So in your example, od6 is a valid worksheetId, but you must discern the other worksheetIds from the spreadsheet's feed.

Building on suman j's answer, here's a way to load multiple sheets, regardless of how many there are, using jQuery.
Load the spreadsheet's main JSON feed, find the url for each sheet in the JSON that's returned, tweak the url a bit, then load that one as well. Then use ajaxStop to take any action that needs to happen after they all finish loading.
$(function(){
var feedurl_start = "https://spreadsheets.google.com/feeds/";
var feedkey = "1c53H0piyduOV6zRwS54A7MDZ3SooJOmj4O52Xd0dyIA";
$.getJSON(feedurl_start+"worksheets/"+feedkey+"/public/basic?alt=json", function(sheetfeed) {
$.each(sheetfeed.feed.entry, function(k_sheet, v_sheet){
var sheeturl = v_sheet.link[0]["href"]+"?alt=json";
sheeturl = sheeturl.replace("basic", "values");
var sheet_title = v_sheet.content["$t"]; // to know which sheet you're dealing with
console.log(sheet_title);
$.getJSON(sheeturl, function(sheetjson){
console.log(sheetjson);
});
});
});
});
// Since you're making multiple AJAX calls, use ajaxStop
// to do whatever you need to do AFTER everything is loaded
$( document ).ajaxStop(function() {
// now all your data is loaded, so you can use it here.
});

Related

Google Sheets Script - save as PDF - Get Name from Cell

Ok, I'm 99% there thanks to a lot of other posts about how to do this.
What am I doing?
I have a google form. When that form is completed and the answers are in the respective google sheet I have another tab that has a template (with a bunch of formulas) that auto-populates pay rates for people. The first formula is the name and it is drive off of the Form Responses tab. The following script hides all other sheets, saves the one tab I want to a PDF, in the folder that I want (then unhides the other sheets).
The problem
When I run this as a test it works perfectly. The PDF that drops into the folder is named with the output from the cell E5 (which is a formula) pulling the person's name. However when I set the trigger to when a form answer comes in then everything is right (all the data on the sheet is right) just the name of the file is showing as "Yes". I'm so confused as to why it is "Yes". If I run the script right afterwards it is the name of the person from the cell E5..... Here is my code.
Is the trigger to fast to pull the response? I don't think it can be as the output on the PDF is right.
function topdf() {
var foldersave=DriveApp.getFolderById('1dSOK6bBCRyEO0_fTuuMFH9sNa4wAWYW8');
var d= new Date();
var request = {
"method": "GET",
"headers":{"Authorization": "Bearer "+ScriptApp.getOAuthToken()},
"muteHttpExceptions": true
};
var key='1-e8dhOOZ5zwfeIBSTSH7yFNKgxVTxwqZvqcTNtQUQrA';
var fetch='https://docs.google.com/spreadsheets/d/'+key+'/export?format=pdf&size=letter&portrait=false'
var ss= SpreadsheetApp.getActiveSpreadsheet();
var sheets=ss.getSheets()
var name=ss.getRange("E5").getValue()
sheets[0].hideSheet()
sheets[1].hideSheet()
sheets[2].hideSheet()
sheets[3].hideSheet()
sheets[4].hideSheet()
sheets[5].hideSheet()
sheets[6].hideSheet()
var pdf = UrlFetchApp.fetch(fetch, request);
pdf = pdf.getBlob().setName(name);
var file = foldersave.createFile(pdf)
sheets[0].showSheet()
sheets[1].showSheet()
sheets[2].showSheet()
sheets[3].showSheet()
sheets[4].showSheet()
sheets[5].showSheet()
sheets[6].showSheet()
}
You're calling getRange() E5 from Spreadsheet ss, which may or may not be the sheet you're looking to get the range from. E5 may be yes from an another sheet. Try
ss.getSheetByName('Sheet1').getRange('E5')

Getting google sheet dropdown values of a cell with sheet api using javascript

I am trying to get all the values from a dropdown list of a cell in google sheet
google sheet dropdown screenshot
however in the json return that i am getting from the sheet api, I only get the selected value and not the list of all the values of the dropdown. How can i the whole list?
the ajax code is:
"GET",
"https://sheets.googleapis.com/v4/spreadsheets/"+sheetId+"/values/Sheet1?key="+apiKey+""
json return screenshot
That's the expected outcome. Sheets API only fetches values which are currently visible on the cells. There is no way to get the hidden whole list. Check Reading and Writing for more info.

Google Spreadsheet API Query Tabs

Can I query my spreadsheet tabs via tab index instead of the tab title or tab id?
https://sheets.googleapis.com/v4/spreadsheets/{my sheet id}/values/'Hero Video Copy Section'?key={secret code}
'Hero Video Copy Section' is the title of the tab, but I want to find the tab via index.
You may refer with this SO thread(quite old question). Given example is if the row number is in A1, and the column number is in A2:
=OFFSET(A1;A1-1;A2-1)
=INDIRECT("R"&A1&"C"&A2)
=INDEX(A1:400000;A1;A2)
Also, you can check this documentation about Google Apps Script which uses an array to log the name of the second sheet.
// The code below will log the name of the second sheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
if (sheets.length > 1) {
Logger.log(sheets[1].getName());
}

I want to copy the format of one Spreadsheet in google docs do another spreadsheet. Is this possible?

I'm working on a feedback system for my math students and have a matrix that shows what points they have gotten on a test.
I have several sheets in this document with one sheet per student. I have also made one spreadsheet per student that i have put in a shared folder on Google Drive where the students only have viewing rights.
What I want to do is copy the entire sheet from my master document to the student's spreadsheet. This is all fine if I use the import range function however with that the formatting doesn't follow.
Is it possible to use a script with the commands getBackgroundColor() and setBackgroundColor() to transfer the backgroundcolors from the master sheet to the students sheet?
I started with something like this:
function getColor() {
var sheet = SpreadsheetApp.openById("0ApOkn5XcO_GDdFd4SDBaaFpLSEUxXy1vN2pnNHBpYnc");
var ss=sheet.getSheetByName("Student1");
var r=ss.getRange("A1:R34");
var color = ss.getBackgroundColors();
That's about as far as I get. Anyone have ideas?

Getting a merged cell width on Google Spreadsheet API

I'm using the Google Spreadsheet API to convert a document containing workers shifts into event calendars.
The only problem is that shifts are represented by merged cells according to days and hours (with days and hours as rows and different work slots as cols), and when I read a certain cell, which is merged and spans over 6 cells, I cannot get the cells certain width or its merged area.
For example:
If I try to get the values between (4C:4E) I will get "Bob, , ," and not "bob,bob,bob", and I cannot even find a way to know how many cells "bob" take.
Do you guys know how can I know how many cells the merged one spread to? Or at least it's total width.
Thanks in advance!
Download from google drive as html, see:
Get FontStyle information from Google spreadsheet into appengine
Drive driveService = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential).build();
File file = driveService.files().get(this.spreadsheetKey).execute();
String downloadUrl = file.getExportLinks().get("application/pdf");
downloadUrl = downloadUrl.replaceFirst("exportFormat=pdf", "exportFormat=html");
downloadUrl = appendWorksheetGid(downloadUrl); // adds "&gid="+sheetGid
HttpResponse resp =
driveService.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl))
.execute();
System.out.println("downloadUrl:"+downloadUrl);
InputStream fileContent = resp.getContent();
extractStyleFromHtml(fileContent,downloadUrl);
extractStyleFromHtml uses Jsoup - (Jsoup impressed me)
It's not possible via spreadsheet API. I was looking for same. Google admits the same in their documentation.
The literal value of the cell element is the calculated value of the
cell, without formatting applied. If the cell contains a formula, the
calculated value is given here. The Spreadsheets API has no concept of
formatting, and thus cannot manipulate formatting of cells.
Related question:
Set cell format in Google Sheets spreadsheet using its API & Python
One alternate way can be to download the doc in excel format programmatically. It will contain formatting information. Then using excel API to extract the info.