Getting a merged cell width on Google Spreadsheet API - 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.

Related

How to get all the filled data in the table in google sheets api?

Please tell me how in the Google Sheets API you can not manually get the range of the entire table located on a certain sheet.
I have a table in which the number of columns can change, but I still have to get information from it.
sheet = service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id, range='???').execute()
You can you sheet.getDataRange() to get range of the visible data on your spreadsheet.
solution:
sheet = service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id,
range='Sheet1',
majorDimension='ROWS').execute()

Finding last written row in Google Spreadsheet API

Is there any way to find the last row you have written in a google spreadsheet in Java?
I tried to do that by having a variable which I keep in another file and I update that every time I do another writing. Is there any other way?
Finding last written row in Google Spreadsheet API
I'll give you a concept first using REST calls but you'll have to apply that in Java.
Exceed the range of cells where you're writing.
So to find last written row between cells A1:A10, use the range A1:A11 or A1:B (using B means all the rows in cell A will be traversed).
Fetch a range of cells, using GET. Parse the response result. And get the length of the parsed values. The length will always indicate the last row since Sheetsv4 ignores blank/empty cells.
So example I have a range of cells between A1:A10. I wrote "hello" in A10. Following the concepts above I do this:
..
xhr.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/'+myspreadsheetId+'/values/Sheet1!A1:A10);
..
xhr.onload = function (oEvent) {
arrayBuffer = xhr.response;
myArray = JSON.parse(arrayBuffer);
console.log("last row is " + myArray.values.length)
.. //output is 'last row is 10'
This part of my XHR request returns 10. Now I know that the last written row in my sheet is A10.
To do this in Java use the solution based on this SO thread.
mService.spreadsheets().values().get("ID_SHEET", "Sheet1!A1:B").execute();
Also check this thread about writing on data cells using Java. I think it offers additional insight as well.

Add a URL image to a cell where the URL is based on input in another cell

I am moving over a worksheet from GoogleDocs that has to be in Excel now.
It is a barcode generator sheet.
The image cell in GoogleDocs has the formula is
=image("www.somebarcodesite.com/image.php?code=" & A2 & "&style=197&type=C128B&width=300&height=50&xres=1&font=3", 3)
I've downloaded as Excel but that has not helped (I think due to text concatenation) and additional rows may be need to be added so it's not enough to embed the image once when downloading.
I've seen VB solutions to put an image in a cell, but I'm not sure it would allow the users to add new rows to the worksheet.
Does anyone know where I should start on finding a scalable way to show the image generated at a url, where the url is generated dynamically based on the input of other cells?

google spreadsheet feeds with multiple worksheets

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.
});

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?