Can read from sample sheet but now from own sheet - java

I'm trying out Google Sheets API from a Java application. I've accessed the file mentioned in the tutorial but I cannot access any file that I have created on my own.
This is the code I'm using:
String spreadsheetId = /*omitted*/;
String range = "Class Data!A1:B";
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
I created a spreadsheet manually in Drive, filled A1:B with strings and copied the id from the URL, looking something like "1IeoY5jY3Su86x1uvgc1yJqEEU-6dd6FdUKo8Yf5J73k" (not an actual ID).
This generates an error 400 Unable to parse range: Class Data!A1:B
I'm guessing that this means that it cannot access my spreadsheet, since the sheet is filled with strings "ab" in these cells.
The sample code has a spreadsheetId that refers to some kind of public document and it is working for that document. I am guessing that I am doing something fundamentally wrong here. I have verified that the document is created with the same credentials as the ones I'm using for the Java application. Any ideas?

Class Data is the name of the sheet in the sample sheet. You should substitute that with the name of the sheet in your spreadsheet. You can see the name of the sheet on the tab near the bottom of the screen. It defaults to Sheet 1.
A1:B is the number of rows and columns to read. It is saying to read all data in columns A and B from rows 1 to the last row. You should swap that with the rows and columns you want to read in your sheet.

Related

How to get the unique identification of the images which are present in excel sheet using aspose java

I am trying to add few images to excel sheet manually. After that I have to read those images in java code using aspose cells library. I can read using aspose as below.
Workbook book = new Workbook("test\\output.xlsx");
WorksheetCollection collection = book.getWorksheets();
Worksheet worksheet = collection.get(0);
PictureCollection pics = worksheet.getPictures();
System.out.println("Total number of pictures in the sheet : "+pics.getCount());
for(int i=0; i<pics.getCount(); i++) {
Picture pic = pics.get(i);
System.out.println("Name of the image at index "+i+" is "+pic.getName());
}
But I have an excel sheet as below in which I added images manually.
I have to read it to a HashMap such that key will be the Code column values and values will the image column values. I want proper correspondence. Since images are not cell based I could not get the Map in that way. So is there any way I can map proper 'Code' column value with proper 'Images' column value.
Thank you in Advance.

How to insert a sheet to Google spreadsheet in a specified location using java

Am inserting a worksheet to Google spreadsheet every week with the help of following command
WorksheetEntry worksheet = new WorksheetEntry();
worksheet.setTitle(new PlainTextConstruct(sheet_name));
worksheet.setColCount(data[0].length);
worksheet.setRowCount(data.length);
URL worksheetFeedUrl = entry.getWorksheetFeedUrl();
service.insert(worksheetFeedUrl, worksheet);
Always its inserting the sheet at last position but i want to insert it at the second position every time....
Is it possible using java? If so please help me...
Thanks in advance...
It is possible with google-apps-script, but not with the spreadsheet API (GData).
insertSheet(sheetIndex)
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#insertSheet(Integer)
Inserts a new sheet in the spreadsheet at the given index. As a side effect, it makes it the active sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);
AFAIK, there is no such function with the spreadsheet API (GData). It is not listed here https://developers.google.com/google-apps/spreadsheets/

Dynamic sheet creation using Aspose cells in Java?

I am using aspose-cells.jar for Excel export in my application.
I am stuck where I need to dynamically create sheets in the template.
My original Excel template contains 2 sheets.
Sheet1 contains the table and pie-chart.
Sheet2 contains the data for the table and pie-chart.
Depending upon the number of sample data selected these sheets has to be cloned i.e. Suppose two dates are selected then:
Sheet1 should contain the graph and pie-chart for first date.
Sheet2 should contain the graph and pie-chart for second date.
And
Sheet3 should contain the data for first date.
Sheet4 should contain the data for second date.
You can use the Worksheets.addCopy method to clone an existing worksheet.
// Open the workbook
Workbook book = new Workbook(srcDoc);
boolean bSomeCondition = true;
// If some condition is true e.g. dates
if (bSomeCondition)
{
// Copy first worksheet
book.getWorksheets().addCopy("Sheet1");
// Copy the second worksheet
book.getWorksheets().addCopy("Sheet2");
}
// Save the workbook
book.save(dstDoc);

How to insert specific column of Excel into Database

I have an Excel sheet with 200+ column names and each column has a header name. I have to fetch all the header names first from the Excel sheet using Java and pass them into Oracle DB. The DB returns only a few header names, say 150, which I need to insert into a table.
My problem is that I am unable to fetch the specific header names (150 out of 200) from the Excel sheet and their values to insert into db.
I tried using a CSV file but it cannot fetch specific column values. Please suggest any solution that I need to follow here.
1) Have you tried to read the csv file like regular text file, then extract the first line, the header row, into a String? Then separate the string by "comma" and you should have an array of string with column headers.
2) Make sure you don't have any "unusual" characters such as "dash", "comma", "quote" in your column header description.
Gook Luck

How to read the values of a dropdown using Jxl API

I have an Excel sheet in which 2 columns have a dropdown with one about 4-5
values and the other with 2 values that can be selected. I would like to read
the value that is there for each row in these two columns. . What is the code
sample to do that? I did browse a while on the net and this forum, but did not
find any answers. I posted in the JExcel Yahoo groups , but no success.
I have added the following code, but this does not help. This code sample just prevents the "common Assertion failed" error
WorkbookSettings settings = new WorkbookSettings();
settings.setSuppressWarnings(true);
Workbook workbook = Workbook.getWorkbook(sis, settings);
The sheet gives an erroneous row count when the dropdown is there. Has anyone been able to read the values selected in the drop down ?
If you have applied an autofilter to your Excel sheet then probably that is responsible for the issue. I would suggest you to please remove the autofilter and then try fetching the value of the combobox.
Cell yourCell = yourSheet.getCell(x,y);
String comboboxValue = yourCell.getContents();
logger.log("value selected in combobx is : " + comboboxValue );

Categories