Select a spreadsheet more intelligently using Google Sheets API? - c#

I have been looking in to the sheets API so that I can update the values within a certain spreadsheet, however I can't find an intelligent way to select it, rather just entries like below:
SpreadsheetFeed feed = service.Query(query);
//selects spreadsheet
SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
Console.WriteLine(spreadsheet.Title.Text);
At the moment it selects the last edited by (SpreadsheetEntry).feed.Entries[0]
and if I wanted the second edited it would be (SpreadsheetEntry).feed.Entries[1] and so fourth.
What I'd like to achieve: (SpreadsheetEntry).feed.Entries["My Spreadsheet 1"] something along those lines. Rather having to loop through all spreadsheets and then matching up a name as that is just to long winded.
Thanks in advance

You can achieve it by selecting your spreadsheet when you make SpreadsheetQuery:
SpreadsheetQuery query = new SpreadsheetQuery();
query.Title = "My Spreadsheet 1";

Related

Parsing excel sheet - cell by cell

I would like to see your help on how to parse an excel file in mvc c# but, accessing its cell address like:
var a = Excel_cell{A1:B1}
model.a = a
I've looked to various plugin like excelfilereader, epplus and also javascript libraries like sheetjs and alike but, they do the same where they parse the whole file with the layout of a table...
My excel file is quite complicated because it has no headers and its data is on the right side.
EG:
I apologize if I put an image here...
My requirement is read an excel template, and save its data to the database.. I dont need to send the file to server because it will not be beneficial in the long run
Im hoping you could help me... An idea how to accomplish this would be really help.
You can try by using Spire.XLS, It's a good library that helps you read and write (this one only with a license) excel files and some other formats, it's easy to use and intuitive, for example this would be the code to get an excel file and read some cells:
//Create a variable to access the excel file
Workbook yourFile = new Workbook();
Stream stream = new MemoryStream(FilePath);
//Load the excel file into the variable
workbook.LoadFromStream(stream);
//Select the worksheet you want to take values from
var firstSheet = workbook.Worksheets.Where(x => x.Name == "Name of the sheet").First() as Worksheet;
string value = firstsheet.Range[A1].Text;
So with this code you have populated a variable with the excel file, a variable with a specific sheet (for example the first one), and then you save the text value of a specific cell of your choice in another variable, let me know if I got it right
EDIT: This is their site and this is the nuget gallery

Enumerating Google spreadsheets?

I'm fiddling with Google Sheets AP v4, starting with this introduction. The API seems pretty clear but I can't figure out how to enumerate existing spreadsheets. Is that not a supported feature for some reason or am I missing something?
You should be able to get the list of sheet names by using this code:
var spreadsheets = service.Spreadsheets.Get(spreadSheetId).Execute();
var titles = spreadsheets.Sheets.Select(s => s.Properties.Title).ToArray();
Sheets get returns a sheet resource which contains a list of sheets within a sheet.
So it's just a matter of getting the sheet you are interested in and you will have a list of the sheets it contains

How to create an excel spreadsheet using a dataset with Npoi

I am completely new to Npoi and I am struggling trying to find some information on how to create a basic spreadsheet from a DataSet.
I have a DataSet being returned that will only ever contain one table I am trying to get the columns and values from these columns into an dynamically generated excel file that I can allow users to download.
So far I have been able to create the workbook and sheet but I can't figure out how to properly populate sheet using the Npoi.dll
My code for creating the workbook (so far) is as follows:
private void CreateWorkbook(DataSet ds)
{
var table = ds.Tables[0];
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet();
foreach (DataColumn col in table.Columns)
{
//seems like I should create the columns for the sheet here
foreach (DataRow row in table.Rows)
{
sheet.CreateRow(); //then populate each column with the approriate data
}
}
}
In addition, and I may be missing something but where is all the documentation Npoi on codeplex mentions, I cannot find anything relevant for Mvc apps dynamically creating a sheet and returning it to the client? What I am trying to accomplish is to create create the file and pass it to the client without storing it on the server.
I'm sure this is basic but I can't figure out where to look for information. I'd appreciate any suggestions.
-cheers
A couple links that got me started:
How to read in XLSX data for editing with NPOI
Creating Excel spreadsheets .XLS and .XLSX in C#
And finally at quick overview of basic functions from the POI documentation like reading a named range, collapsing rows, etc. As NPOI is a close match to POI the guide is fairly effective.
Busy Developers' Guide to HSSF and XSSF Features

C# Linq To Excel Getting Table Data Not Starting In The First Row

I'm developing a MVC 4 web application and one of the requirements is to allow users to upload an excel file which is in a standard format and extract data and save that to a database. I have used linq to excel to read data off of the excel. This works fine provided that the table that I'm extracting data from starts from the first row of the excel sheet.
var details = from c in excel.Worksheet<ContributionScheduleExcelFormat>() select c;
Now my question is how can we still return the same data if say the table headers starts on the third row? Basically some extra information needs to be reflected on the first two rows so that's why my the table in the excel sheet needs to start from the third row now. I believe there is a function already available to get data from a range of cells.
var details = from c in excel.WorksheetRange<ContributionScheduleExcelFormat>(startRange, endRange) select c;
But how would I get the endRange value?
I'm new to linq to excel so please any assistance would be greatly appreciated. Thanks in advance.
For the sake of others that may have this same issue:
Turns out that you can't actually do this at the moment. The only solution is to specify an end range that you know your excel sheet data will not exceed. For example:
var details = from c in excel.WorksheetRange<ContributionScheduleExcelFormat>("A3", "G16000") select c;
It's not pretty at all and personally just looking at it makes me feel uncomfortable but that's the only way right now.

How to Get Selected Sheet Names in Excel

I am trying to get names of the selected sheets in Excel. I have 4-5 worksheets in my excel file. User is supposed to select two of them and then my application scans specific columns and compare values. However I could not find a way in C# to get names of the worksheets when user selects more than one sheet. User can also delete these selected worksheets via the application. Any ideas?
Using VSTO:
var sheets = Application.ActiveWindow.SelectedSheets;
var names = new List<string>();
foreach (Excel.Worksheet sh in sheets)
{
names.Add(sh.Name);
}
One way is to use Spreadsheetgear: Link to Spreadsheetgear website
This is the easiest solution to manipulating Excel workbooks I tryed. But quite expensive.
If you're after a free solution, have a look at Excel Package Plus: http://epplus.codeplex.com/
It should have all the features that you require.

Categories

Resources