Reading cells which have formulae data using OpenXML API - excel

I'm using the OpenXML API to read an excel file. I have a wrapper for the API, so I can directly read the rows and cells in Excel. There are two columns whose values are generated using a formula as follows.
=B12&"."&C12&"#dtestorg.com"
Basically, what it does is - create the email address using the first name and last name cell values for a user.
When I read this cell as follows, I read it as empty:
row.XCells[6].GetValue()
Is there a specific way to read the cells generated using formulae, in OpenXML?

Essential XlsIO is an option for reading & modifying Excel documents with formulas.
For example, let us consider you have an Excel sheet as shown below.
Step 1: Create a console application
Step 2: Add reference to Syncfusion.XlsIO.Base and Syncfusion.Compression.Base assemblies. These assemblies can be downloaded from NuGet too.
Step 3: Copy & paste the following code in the main method
//Create an instance of Excel application instance
using (ExcelEngine engine = new ExcelEngine())
{
//Open an existing Excel workbook
IWorkbook workbook = engine.Excel.Workbooks.Open(#"..\..\Template.xlsx");
//Access the worksheet with the name "Sheet1"
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
//Access the range/cell "C2" from the worksheet
IRange range = worksheet["C2"];
//Access the Text to be displayed in the cell
Console.WriteLine("Display Text: " + range.DisplayText);
//Access the Formula of the cell
Console.WriteLine("Formula:" + range.Formula);
}
The whole suite of controls is available for free (commercial applications also) through the community license program if you qualify. The community license is the full product with no limitations or watermarks.
Note: I work for Syncfusion.

Related

Is it possible to copy, paste, and delete data from one sheet to another in office scripts?

I have a document saved on SharePoint that documents testing.
Once testing has been completed I would like to move the tests (entire row) to a separate sheet to archive the test schedule, in the next empty row going down, and because its now archived delete it from the submission sheet.
Both sheets are within the same workbook. The number of rows differ each time the script is run therefore the script code would need to work based on a selection input by the user (either prior to running the script or prompted by the script).
I managed this in VBA. I can't locate a viable alternative to the selection function. Is there any way of translating the below VBA code to Office script so the same thing happens in Excel online?
Sub MoveCompletedTests()
Selection.Copy Sheets("Archive - ATL staff only").Range("A1048576").End(xlUp).Offset(1, 0)
Selection.Delete
End Sub
I have a button on the "Sample submission" sheet that runs the above code on the selected range on that sheet moving it to the "Archive - ATL staff only" sheet.
I attempted to use the script recorder but the script didn't allow for the selection to be dynamic enough, it coded for the cell range rather than just selection.
I think that something like getSelectedRange should work (at least, if you are not working with several selected ranges at once):
function main(workbook: ExcelScript.Workbook)
{
let destination: ExcelScript.Range;
let source = workbook.getSelectedRange().getEntireRow();
let archive = workbook.getWorksheet("Archive - ATL staff only");
if(archive.getUsedRange() == undefined) {
// if the archive sheet is empty, use the first row as a destination
destination = archive.getRange("1:1");
}
else {
// ... otherwise, use the next row after the last used
destination = archive.getUsedRange().getRowsBelow().getEntireRow();
}
destination.copyFrom(source, ExcelScript.RangeCopyType.values);
source.delete(ExcelScript.DeleteShiftDirection.up);
}
p.s. To be clear, in case of VBA a Selection is a property of Application. But in case of ExcelScript it's a method of a Workbook object. This can be a bit confusing because there is an ExcelScript.Application interface, which has no mention of Selection.
You can try the code here:
function main(workbook: ExcelScript.Workbook) {
//Assign the source variable to the selected range
let source: ExcelScript.Range = workbook.getSelectedRange()
//Assign the archive variable to the Archive worksheet
let archive: ExcelScript.Worksheet = workbook.getWorksheet("Archive - ATL staff only");
//Set the destination range in the archive sheet
let destination: ExcelScript.Range = archive.getRange("A:A").getExtendedRange(ExcelScript.KeyboardDirection.up).getLastCell().getOffsetRange(1,0)
//Determine if the offset range is set to A2 in the Archive sheet.
//If so, determine if A1 has a value. If it does not, update the
//destination range to cell A1.
if (destination.getAddress() === "'Archive - ATL staff only'!A2" ){
if (destination.getOffsetRange(-1,0).getValue() === "") {
destination = destination.getOffsetRange(-1,0)
}
}
//Copy the selected source range to the destination range
destination.copyFrom(source, ExcelScript.RangeCopyType.all);
//Delete the data in the source range
source.delete(ExcelScript.DeleteShiftDirection.up);
}
This code determines the last cell based on the data in column A in the archive sheet. This can be useful if the used range of the archive sheet has data somewhere, but column A does not. In this scenario, this code would still update correctly.
Also, this code doesn't copy and delete the entire row. So if you intend to use a selection smaller than the entire row, this code may work better.

Is there a way to edit crosssheet references using the api?

Developing a workflow in which everyday a new spreadsheet is uploaded to the workspace. A second tracking sheet uses a cross sheet reference to populate several columns. I am attempting to automate the upload process and if I am able to edit an existing cross sheet reference I can just have it point to the new sheet as opposed repopulating the cells with a new reference.
Given that the reference editor in the app is capable of changing which sheet and what range is referenced, it makes sense to me that there would be some way for the api to do this as well. I have read through the documentation here: http://smartsheet-platform.github.io/api-docs/ and found nothing that would allow me to edit the reference.
I am hoping that I am missing something obvious.
I am able to get the list of references and retrieve specific ones using the code provided in the documentation.
I also am able to change the cells in the given rows if necessary, but that adds a level of complexity that I would like to avoid.
Here is the code I use to retrieve and add a cross sheet reference
Wrksps = get_workspace_list()
for space in Wrksps:
if space.name == 'QC':
qc_space = space
QC_sheets = get_sheet_list(qc_space.id,'w')
for sheet in QC_sheets:
if sheet.name == 'QC Active Issues':
active_sheet = sheet
active_sheet = get_object(active_sheet.id, 's')
issue_sheet = get_object(Referenced sheet ID, 's')
for col in issue_sheet.columns:
if col.title == 'Work Order ID':
WO_col = col
if col.title == 'Unstartable':
last_col = col
xref = smartsheet.models.CrossSheetReference({
'name': 'Sample Time Log Sheet Range 1',
'source_sheet_id': Referenced sheet ID,
'start_row_id': ROW ID,
'end_row_id': ROW ID
'start_column_id': start_col.id,
'end_column_id': last_col.id
})
result = smart_sheet_client.Sheets.create_cross_sheet_reference(active_sheet.id, xref)
print(result)
result = smart_sheet_client.Sheets.list_cross_sheet_references(active_sheet.id)
That's awesome you are creating this automation for your work! We don't yet have the capability to edit cross sheet references via the Smartsheet API. Right now it is only possible to create them and get the current cross sheet references for a given sheet.
To work around this you could create a new cross sheet reference on the sheet in the same cell. Unused cross sheet references on a sheet are automatically deleted from the sheet after two hours. Or you could update the cells on the sheet being referenced with new data.
I can pass along your use case and how you are looking to edit existing references.

Matlab - get formula from excel with xlsread?

In the documentation doc xlsread it is described that one can use [num,txt,raw] = xlsread('example.xls') to pull data from excel sheets into matlab.
I have a sheet containing formulas to be copied to other sheets, however xlread will take the interpreted formula value instead of the formula itself. For example one formula is =AVERAGE(B8:V8) which is what I would like to pull from the sheet programmatically, but instead excel returns the value 0.810 which is what the formula would return.
Is it possible to extract the formula in any way with matlab?
It is not possible with xlsread only.
One example of using a COM Excel object:
Let's use a simple excel sheet for example, containing text, values and formula :
Then the following code:
xlfile = 'test1.xlsx' ;
xlRange = 'B3:C6' ;
exl = actxserver('excel.application'); %// Create a COM server
exlFile = exl.Workbooks.Open( [pwd '\' xlfile] ); %'// Open the file
exlSheet1 = exlFile.Sheets.Item('Sheet1'); %// Choose the worksheet
strFormula = exlSheet1.Range(xlRange).Formula %// Read the full range
Yields a nice cell array :
strFormula =
'This is text' 'hello'
'this is value' '12.5'
'this is value' '29'
'this is formula' '=AVERAGE(C4:C5)'
If you know directly the address of a specific cell, you return a simple string:
cellFormula = exlSheet1.Range('C6').Formula %// Read a single cell
cellFormula =
=AVERAGE(C4:C5)
It is not possible using xlsread, you have to use one of the APIs available to read excel files or access excel. To my knowledge the choices are:
Apache POI which can easily be used via the Java Bridge. An example can be found here
Similar to the Java Bridge, recent versions of Matlab can access Python Libraries like openpyxl. This requires at lest Matlab 2014b
You can access the Excel Application via COM or .NET

EPPLUS - Rename Cell

I'm using EPPLUS to build custom Excel spread sheets. One functionality that I'm missing is to change the name of a cell. Is this somehow possible with EPPLUS? If not are there any other ways of doing it?
Suppose that book is your workbook and sheet is your worksheet. Let try this code snippet
var cell = sheet.Cells["C2"]; // Cell or Range you want to name
sheet.Names.Add("The_Name_Here", cell);
The name will be added to sheet. If you want to access the name in the whole workbook, try:
var cell = sheet.Cells["C2"]; // Cell or Range you want to name
book.Names.Add("The_Name_Here", cell);
Depend on your need the scope of the name can be sheet.Names or book.Names.
I don't know how to modify an existing name but sure that 1 cell/range may have multiple names.

creating new sheet and moving sheets between spreadsheet in google docs

I have 2 requirements:
1) I want to create a new sheet from existing sheet in current spreadsheet programmatically
2) I want to copy one sheet from one spreadsheet to another spreadsheet programmatically
Help appreciated.
Since you have not specified the environment/language. I think the easiest way to do this is using Apps Script as it's built-in in Google Spreadsheets.
Here is a sample code that does this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('SheetName');
sheet.copyTo(ss).setName('NewName'); //copy to the same spreadsheet.
//get a different spreadsheet (you can get its id in the url)
var os = SpreadsheetApp.openById('any-spreadsheet-key-that-you-can-edit');
//copy sourceSheet from one spreadsheet to another
sheet.copyTo(os).setName('AnotherName');
}
To run this, just open the spreadsheet with the sheet you want to copy, click the menu Tools > Script editor.. Paste the code, Save and Run > myFunction

Resources