Finding last written row in Google Spreadsheet API - google-sheets-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.

Related

Vba email generator, subject from excel spreadsheet

I have a vba code that generates an email. I would like the subject to be the data from the first and last cells in my list. The thing is, my list isnt of a set length, sometimes it contains 5 pieces of data sometimes 8 etc. How do i tell vba to pick the first and last cell no matter the length of the list?
thanks
For me, best practice is to just have cells on your sheet that calculate the first and last row (different ways you can do that), then give those cells a range name such as FirstRow and LastRow. In your vba then you refer to these cells to make your code dynamic.
e.g:
firstRow = Range("FirstRow)
lastRow = Range("lastRow")
test = range(cells(firstRow,lastRow))
-- Note I have not written VBA in many many years so am writing the above from memory so it may be not be exact.
Of course you can do it all entirely in VBA using the xlDown method mentioned previously but I prefer the transparency of it being on the main page so that easily spot if something breaks.
Range("A1").End(xlDown).Value
Where the cell is where you want to start and the End part moves all the way to the end

Updating a cell value with unknown range (Google SpreadSheets API v4)

So, i've made an application that fetches data from Spreadsheet with a rest api call.
This data is being mapped from the ValueRange object to a list of custom object and cached in the application for 10 minutes.
I'd like to be able to update a single cell of a specified row of which I do not know the range value (for example 'E9') of.
Is there a good way to loop through the items in the spreadsheet and update a cell value where another field value matches?!
public static void UpdateQuantity(string nameToMatch, int quantity)
{
// find row where column A matches nameToMatch
// update value of column B with quantity
// finally clear cache to get the new data
CacheHelper.ClearCache();
}
If else, when iterating through rows in spreadsheet, how do I know the current row number so I would do a REST call to update the specific cell?
Thanks in advance.

Detecting last row and calculating total rows on page

I am currently working with a client that generates their invoices manually in Excel. I have used epplus to generate this file automatically for them.
One of the requirements I have is that each page should have a border around it and start with a header that is about 7 rows long (and each row has different heights based on what I have to put in them). In order to do this, I need to detect when I am at the end of a page so that I can pause my data printing and print out the header, then print out all the data for that page and print the border on the page based on how many rows are on the page.
This was a bit easier when the row heights were static because I could just assume that based on the page settings, that there were a specific amount of rows per page. However, some of the rows on the invoice can have multiple lines which means that WrapText = true. This obviously alters the numbers of rows on the page which throws all my formatting off.
Is there a way to calculate how many rows are on a page and detect when I am on a row where the page ends?
Thanks in advance for any help
To get the row count, and column count, I used the following code:
// get the first worksheet in the workbook
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
//Gets the row count
var rowCnt = worksheet.Dimension.End.Row;
//Gets the column count
var colCnt = worksheet.Dimension.End.Column;

Find exact match of text in excel array (from another sheet) and display a specific cell value from the same row

I am comparing two separate spreadsheets. I need to write a formula that will find the exact text in the other sheet (if it exists), and will then display a value in the corresponding row only 5 cells over.
I have tried various iterations of Index Match to no avail. Any help would be greatly appreciated. The sheets are formatted roughly the same way.
Let me know if I am not being clear enough.
I used this formula, but it is displaying data regardless of whether or not the cell data matches data in the other sheet's row.
=IF(ISERROR(INDEX(product_performance!$A$2:$E$850,SMALL(IF(product_performance!$A$2:$A$345050=$A$13,ROW(product_performance!$A$2:$A$3450)),ROW(1:1)),8)),"",INDEX(product_performance!$A$2:$H$3450,SMALL(IF(product_performance!$A$2:$A$3450=$A$13,ROW(product_performance!$A$2:$A$3450)),ROW(1:1)),8))
You cannot ask for the 8th column within $A$2:$E$850 because that only references 5 columns. The formula you are using seems to be for returning a first, second, third, etc. matching value but your narrative gives no mention of multiple returns. For single returns, a VLOOKUP function or INDEX/MATCH function pair would be best.
'vlookup
=IFERROR(VLOOKUP($A13, product_performance!$A$2:$H$3450, 8, FALSE), "")
'index/match
=IFERROR(INDEX(product_performance!$A$2:$H$3450, MATCH($A13, product_performance!$A$2:$A$3450, 0), 8), "")
=IFERROR(INDEX(product_performance!$H$2:$H$3450, MATCH($A13, product_performance!$A$2:$A$3450, 0)), "")
For multiple returns, throw out the SMALL function in favor of the newer AGGREGATE¹ function. As a standard (non-array) formula,
=IFERROR(INDEX(product_performance!$H:$H, AGGREGATE(15, 6, ROW($1:$3450)/(product_performance!$A$1:$A$3450=$A$13), ROW(1:1))), "")
Fill down for subsequent returns.
The IFERROR function has been around sinbce xl2007 and drastically reduces the formula needed for error control of IF(ISERROR(....

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.