Write array formulas - ms-office

To copy single formulas from an area to another, we could use directly rangeTarget.formulas = rangeSource.formulas once rangeSource.formulas is loaded. However, this method does not seem to apply to array formulas.
For example, A1:A8 contains an array formula {=B1:B8+10}, and the following code attempts to copy this array formula to C1:C8:
function test () {
Excel.run(function (ctx) {
var r0 = ctx.workbook.worksheets.getItem("Sheet1").getRange("A1:A8");
r0.load(["formulas"]);
return ctx.sync().then(function () {
var r1 = ctx.workbook.worksheets.getItem("Sheet1").getRange("C1:C8");
r1.formulas = r0.formulas;
});
});
}
Here is the result (Excel Online and Excel for Windows):
So does anyone know what's the correct way to copy an array formula?
Additionally, I don't know how to enter an array formula manually in Excel Online, the shortcut Ctrl+Alt+Enter does not seem to work (on Mac keyboard).

Array Formulas are not currently supported by the Office.js Excel APIs. However, please feel free to suggest / vote for it on https://officespdev.uservoice.com/
~ Michael Zlatkovsky, Developer on Office Extensibility Team, MSFT

Related

Select a discontiguous range

I'm building an office-js add-in for Excel. I need to select two non-adjacent cells (e.g A1 and C3). The following code works to select the multi-cell range starting at A1 and ending at C3.
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getItem('sheet1');
var range = sheet.getRange('a1:c3');
range.select();
return ctx.sync();
});
However, I'm looking to select only the two cells (A1 and C3). In VBA the syntax is
worksheets("sheet1").range("a1,c3").select
But I cannot find anything analogous in office-js. I've tried as similar syntax with office-js:
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getItem('sheet1');
var range = sheet.getRange('a1,c3');
range.select();
return ctx.sync();
});
but it fails with: {"code":"InvalidArgument","message":"The argument is invalid or missing or has an incorrect format.","errorLocation":"Worksheet.getRange"}
An API to work with discontinuous ranges is not yet available in Office.js. We are working on it and are finalizing the design right now. In the meantime, you will have to create separate range objects for the two cells and operate on each of them with duplicate commands.
Actually on the latest insiders fast (16.0.9327.2006 ) deployed just a few days ago you can actually try our implementation for Areas (aka discontinuous ranges. )
please make sure to use our preview cdn to test this.(https://appsforoffice.microsoft.com/lib/beta/hosted/office.js)
but basically you can do things like:
function run() {
return Excel.run(function (context) {
var range = context.workbook.getSelectedRange();
range.load("address");
return context.sync()
.then(function () {
console.log("The range address was \"" + range.address + "\".");
});
});
}
and you will see that if you select a non-continuous range you will get something like: "Sheet1!C6:C14,Sheet1!F12:H22".
you can pass a similar string on the getRange method to create an Area and simultaneously format it etc.
Please give it a try and send us your feedback! thanks!
context.workbook.getSelectedRange()
is used for contiguous ranges.
If you want to get the range for a discontiguous range you should use:
context.workbook.getSelectedRanges()

How can we include the cell formula while export to excel from .rdlc

In my rdlc report have following columns
SlNo, Item, Uom, Qty, Rate, Amount
Here the Amount field is a formula (Rate*Qty)
The report is working fine, and when i export to excel also displaying the values are correctly.
But my problem is, after export to excel, when i change the Qty or Rate columns in excel file the Amount is not get changed automatically, because the formula is missing in the excel cell.
How can we include the formula in Amount column while export to excel from .rdlc?
I'm afraid that this required behaviour isn't really possible by just using the rdlc rendering.
In my search I stumbled upon this same link that QHarr posted: https://social.msdn.microsoft.com/Forums/en-US/3ddf11bf-e10f-4a3e-bd6a-d666eacb5ce4/report-viewer-export-ms-report-data-to-excel-with-formula?forum=vsreportcontrols
I haven't tried the project that they're suggesting but this might possibly be your best solution if it works. Unfortunately I do not have the time to test it myself, so if you test this please share your results.
I thought of the following workaround that seems to work most of the times, but isn't really that reliable because the formula sometimes gets displayed as full-text instead of being calculated. But I guess this could be solved by editing the excel file just after being exported, and changing the cell properties of this column containing the formula or just triggering the calculate.
Using the built-in-field Globals!RenderFormat.Name you can determine the render mode, this way you can display the result correctly when the report is being rendered to something different than Excel. When you export to Excel, you could change the value of the cell to the actual formula.
To form the formula it's self you'll need to figure this out on your own, but the RowNumber(Scope as String) function can be of use here to determine the row number of your cells.
Here is a possible example for the expression value of your amount column
=IIF(Globals!RenderFormat.Name LIKE "EXCEL*", "=E" & Cstr(RowNumber("DataSet1")+2) & "*F" & Cstr(RowNumber("DataSet1")+2) ,Fields!Rate.Value * Fields!Qty.Value )
Now considering that this formula sometimes gets displayed as full-text, and you'll probably have to edit the file post-rendering. If it's too complicated to determine which row/column the cell is on, you could also do this post-rendering. But I believe that the above expression should be easy enough to use to get your desired result without having to do much after rendering.
Update: The following code could be used to force the calculation of the formula (post rendering)
var fpath = #"C:\MyReport.xlsx";
using (var fs = File.Create(fpath))
{
var lr = new LocalReport();
//Initializing your reporter
lr.ReportEmbeddedResource = "MyReport.rdlc";
//Rendering to excel
var fbytes = lr.Render("Excel");
fs.Write(fbytes, 0, fbytes.Length);
}
var xlApp = new Microsoft.Office.Interop.Excel.Application() { Visible = false };
var wb = xlApp.Workbooks.Open(fpath);
var ws = wb.Worksheets[1];
var range = ws.UsedRange;
foreach (var cell in range.Cells)
{
var cellv = cell.Text as string;
if (!string.IsNullOrWhiteSpace(cellv) && cellv.StartsWith("="))
{
cell.Formula = cellv;
}
}
wb.Save();
wb.Close(0);
xlApp.Quit();

Automatically move the screen to view the selection with an Excel add-in

I want to make a function to select a range in a workbook and visualise that range on the screen. My test shows that select() does select a range, whereas it does not always adapt automatically the screen to show the selection.
For example, I try to select and show Z10:Z10 under Excel for Windows by the following code. But after the selection, the screen does not move:
Office.initialize = function(reason) {
$(document).ready(function() {
app.initialize();
$('#testSelect').click(function() { select("Z10:Z10", "Sheet1") });
});
};
function select (rangeAddress, sheetName) {
Excel.run(function (ctx) {
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
range.select();
return ctx.sync();
});
}
Could anyone help?
This appears to be a bug (or regression) in the APIs. The team is looking into it right now. In terms of a workaround, what Michael Saunders suggested above is probably your best short-term bet.
Update: This is indeed a bug (or regression) in the Excel desktop client. We have fixed it today, so for Office 365 / Click-to-Run users, it should be available with the next update (i.e., within a couple months).
~ Michael Zlatkovsky, Developer on Office Extensibility Team, MSFT
One option here in the callback after your code completes is to bind to the range and then navigate to it:
Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Matrix, {id: "myBinding"}, function(asyncResult){
Office.context.goToByIdAsync("myBinding", Office.GoToType.Binding, function(asyncResult){});
});
-Michael Saunders, PM for Office add-ins

Missing method in JavaScript API for Excel: copy a sheet

Since there is no way to cancel the changes made by an add-in, we need to provide users with backup options.
The current version of JavaScript API for Excel doesn't have a method for copying a sheet with its data and formatting.
Does anyone know of any workarounds or plans for adding such method?
There are indeed currently no way easy way of duplicating a worksheet and so feel free to request it on the Office Extensibility Platform's UserVoice. While such an API may be coming in the future, you could in the meantime add a new worksheet using worksheetCollection.add(), grab a worksheet's used range using the worksheet.getUsedRange() method and copy its values to another sheet.
Your code would then look something like this :
function duplicateSheet(worksheetName) {
Excel.run(function(ctx) {
var worksheet = ctx.workbook.worksheets.getItem(worksheetName);
var range = worksheet.getUsedRange();
range.load("values", "address");
var newWorksheet = ctx.workbook.worksheets.add(worksheetName + " - Backup");
return ctx.sync().then(function() {
var newAddress = range.address.substring(range.address.indexof("!") + 1);
newWorksheet.getRange(newAddress).values = range.values;
}).then(ctx.sync);
});
}
Let me know how that works out for you.
Gabriel Royer - Developer on the Office Extensibility Team, MSFT

Google Doc referencing another sheet

After speaking to Google Enterprise Support they suggested I create a post on Stackoverflow.
I have a Google Doc sheet with a list of stores (sheet A) and I'm trying to reference another sheet (sheet B) to VOID specific stores.
What I'm going to accomplish is if a store name on the void sheet is entered into sheet A it will automatically convert the store name to VOID.
Google support believes an IF statement would be the beginning to the solution, they weren't able to help beyond this.
For anyone's time that comes up with a solution, I'd be happy to buy you a couple Starbucks coffees. Your support means a lot.
make it simple using Google Scripts. (Tutorial)
To edit scripts do: Tools -> Script Editor
and in the current add this function
EDIT:
Well, you need to make a trigger. In this case will be when the current sheet is edited
Here is the javascript
function onEdit(event) {
// get actual spreadsheet
var actual = event.source.getActiveSheet();
// Returns the active cell
var cellSheet1 = actual.getActiveCell();
// get actual range
var range = event.source.getActiveRange();
// get active spreadsheet
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// get sheets
var sheet_two = activeSpreadsheet.getSheetByName("Sheet2");
range.copyValuesToRange(sheet_two, range.getColumn(), range.getColumn(), range.getRow(), range.getRow());
// get cell from sheet2
var cell = sheet_two.getRange(range.getValue());
cell.setValue(cellSheet1.getValue());
// display log
Logger.log(cellSheet1.getValue());
}
if you want to test it you can check my spreadsheet, there you can check that all data thats is inserted in sheet1 will be copied to sheet2

Resources