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
Related
I need to add selected set of sheets into a pdf using Microsoft.Office.Interop.Excel library in C#. I have been searching throught he net to figure out how to this. But I haven't been able to find anything helpful. Can someone help me with this. Found following statement to convert a given sheet to a pdf. But it looks like, it cannot append list of excel sheets into one pdf when we provide sheet names.
xlws.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, endPath);
Help please.
You can use the Worksheet.ExportAsFixedFormat method.
Example:
string exportFilePath = #"C:\Desktop\Test.pdf";
activeWorksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, exportFilePath);
Or you can also use the Workbook.ExportAsFixedFormat method
string exportFilePath = #"C:\Desktop\Test.pdf";
activeWorksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, exportFilePath);
References:
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.exportasfixedformat.aspx
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.exportasfixedformat.aspx
I am trying to remove the range through aspose but no luck. Actually i have created a range in excel but while exporting the excel when there is no data to insert into the excel and when we select the range it always select the empty rows. So i want to update the range to the header only or first delete the range and then again adding that.
Please suggest?
Thanks in advance.
You may use any of the NameCollection.Remove or RemoveAt methods to remove a particular Named Range from the collection. Please review the following piece of code as well as the details article on Named Ranges in prespective of Aspose.Cells for .NET APIs.
var book = new Workbook(dir + "book1.xlsx");
var names = book.Worksheets.Names;
names.Remove("range");
In case you still face any difficulty, please share your sample spreadsheet in Aspose.Cells support forum.
Note: I work with Aspose as Developer Evangelist.
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";
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.
This topic is related to: What object type are multiple selected sheets in Excel?
To give an overview of the problem, I run some code that changes my selection in Excel, and I want to return the selection to what it originally was (I call this "originalSelection"). In most instances, you can just invoke the Select() method on the originalSelection.
var originalSelection = ExcelApp.Selection;
originalSelection.GetType().InvokeMember("Select", System.Reflection.BindingFlags.InvokeMethod, null, originalSelection , null);
With multiple sheets selected, the selection type is always of a range (it's what Excel defaults to). However, if you have multiple sheets selected, you can run into errors when trying to invoke Select again. You need to do some dancing around to get things to work.
If multiple sheets are selected, but not all the sheets, you can do the follow:
selectedSheets.Select();
activeSheet.Activate();
originalSelection.Select(); //this was casted to an Excel.Range
However, if all the sheets are selected, the activeSheet.Activate() line deselects all the other selected sheets. This also happens if you try it natively using the UI.
I was wondering if there is a way to pragmatically mimic shift-selecting sheets one by one through the code? Closest stuff I've found is stuff with range groupings, but nothing for sheets.
I tried to keep my overview brief, but if you need more clarification on what I'm doing, just ask.
So I figured out a way programatically select sheets.
You can create a string array of names, and use the ordering of the array to get a collection of the sheets. Select this collection, and you should have all the specified sheets selected.
String[] sheetsToBeSelected = {"Sheet3","Sheet1","Sheet2"}; //Note, Sheet3 is the first item in this array
excel.Workbook workbook = ExcelApp.ActiveWorkbook; //get your Excel application however you want
excel.Sheets worksheets = workbook.Worksheets; //get all the sheets in this workbook
//This gets a collection of the sheets specified and ordered by the array of names passed in.
//Just call select on this collection, and the first sheet in the collection becomes the active sheet!
((excel.Sheets)worksheets.get_Item(sheetsToBeSelected)).Select();