Getting excel range from argument example javascript api - excel

I am trying to write a custom function in scriptlab to check if a value is in a selected range, however I cannot seem to figure out how to actually accept that range as the function argument. The example function I have created is below (where I thought range would equal something like "A1:D1"):
/** #CustomFunction */
async function exists(range): any {
const sheet = context.workbook.worksheets.getActiveWorksheet()
sheet.getRange(range)
...
}
This however results in a $CALC! error. Any tips or ideas?

Custom Function doesn't support to call office.js API yet. Please stay turned, that will be enabled soon.

Related

Tabulator setHeaderFilter before inital AJAX call?

I am trying to set a default headerFilter value before the table loads. I tried all the way up to tableBuild callback. Still getting 2 calls to the API, one for the regular non-filtered data then one with the default filter. The column initialValue and defaultValue params do not seem to affect filterHeaders which is why I am going down this rabbit hole.
tableBuilt: function () {
console.log("tableBuilt");
this.setHeaderFilterValue("status", "Inactive");
}
Try the initialHeaderFilter value during table creation.
initialHeaderFilter: [{field: 'fieldName', value: 'filter value'}]
http://tabulator.info/docs/4.6/filter#header
Here is a working example.
https://jsfiddle.net/nrayburn/r319zaep/40/

PDFTron: What is the proper way to find date fields in a PDF form

[PdfTron 5.2]
I have a PDF form with text and date fields. I want to find the date fields.
I can get the actions of the field with getActions().
field.getActions()
returns
{"F":[{"yc":"JavaScript","Gt":"AFDate_FormatEx(\"dd.mm.yyyy\");"}],
"K":[{"yc":"JavaScript","Gt":"...);","ey":null}]}
As you can see, the date is in actions.F[0].Gt. But checking actions.F[0].Gt
for "AFDate" seems wrong, that's too low-level.
Is there a better API function to find out, that I have a date field?
Thank you.
You are correct. The Gt property is obfuscated and minified which is volatile and not meant to be used. If you require an API, you should refer to our documentation. Everything should be available there except a few (one of which will be used below), but feel free to contact us if you do need help!
Unfortunately, there is no API currently to get that type. From my limited understanding, the "type" of a field is determined by the attached actions and not simply a specific type or flag. This suggests all fields are just text fields with special formatting actions to make it look and feel like its a date or numeric field.
In this case, you will have to check the formatting action (F) as you have already noticed for the date formatting function (AFDate_FormatEx). To get the JavaScript from that action, you should use the javascript property on the action which was not in the documentation. However, you can see it if you console log the action.
Here is an example:
const dateActionJs = /.+:"AFDate_FormatEx\(.*/;
instance.docViewer.on('annotationsLoaded', () => {
const annotations = annotManager.getAnnotationsList();
annotations.forEach(annot => {
const actions = annot.getField().getActions();
if (actions.F && actions.F[0] && actions.F[0].javascript && dateActionJs.test(actions.F[0].javascript)) { // F = Format Action
console.log('Found Date');
}
});
});
Let me know if this helps!
EDIT: You can search for AFDate instead of AFDate_FormatEx which will be sufficient.

Find available range in Excel in JS API

I have a requirement in my add in where I need to insert a table in Excel. But I need to know if the available cell range is empty for my table to insert there. So I have to check that if my cursor is at A1, do we have available space from A1 to A15 and there is no existing data there.
I tried getRangeByIndexes() function but this just gives a range. I can't find a function that returns a boolean for my requirement.
Excel will always have "space" to insert data/table. I guess you want to make sure the cells are in fact empty before inserting table? If so, you can get getSelectedRange() API and use getOffsetRange() to select both the selected range (load just the address and values) and the range associated table dimension (load just values). You can then check to make sure values are empty before inserting.
Another way could be to check worksheet.getUsedRange() to ensure the used range is just A1 (empty worksheet). This only works if you are ensuring that the whole worksheet is empty (not just the table's address).
There are a number of ways of doing this. I think the best one is to use a combination of getSelectedRange, getAbsoluteResizedRange, and getUsedRangeOrNullObject
await Excel.run(async (context) => {
let numRows = 10;
let numColumns = 3;
let range = context.workbook.getSelectedRange().getCell(0, 0).getAbsoluteResizedRange(numRows, numColumns);
let usedRange = range.getUsedRangeOrNullObject(true /*valuesOnly*/);
await context.sync();
if (usedRange.isNullObject) {
console.log("The range is empty");
} else {
console.log("Sorry, it's already taken")
}
});
You can try this snippet live in literally five clicks in the new Script Lab (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/Zlatkovsky/4835778375bb5f5b9d49bff4f5d522f0. See more info about importing snippets to Script Lab.
The objects returned by *OrNullObject-sufficed methods are a bit unusual. If you've not run into them before, I recommend you read about it in our docs. I also go into great detail about it in my book, "Building Office Add-ins using Office.js" (with all profits to charity), in a chapter entitled "9. Checking if an object exists".

Google Sheets Script Parameter for e.g. Button

Is it possible to pass parameters on a signed script?
I have a spreadsheet and made a couple of buttons signing functions to each.
Sample function:
function foo(){
return "bar";
}
Calling this function on a cell
=foo()
returns me bar, signing this function to a button
foo
returns me nothing but of course it works. I mean the script cant return a string to an image (whats actually my button). Anyways...
Sample function with parameter:
function foo(bar){
return bar;
}
calling the script in a cell
=foo('hello world')
returns me hello world as expacted
Signing this to my button
foo('hello world')
causes an error because the button cant find the script. He's searching for a function name foo('hello world') but (...) has non to do with the function.
So how can I pass params when signing script?
At the moment I have 26 functions (all the same, only 1 param changes) to solve this. But with passing a parameter I could do all this with 2 functions instead of 26.
You can't pass parameters when using a custom function via assigning it to a drawable image. It only works when the custom function is called from a cell.
Possible workarounds is to store your parameters on certain sheets and cells then retrieve them via
getRange() and getDisplayedValue().
Other possible workarounds are posted in a Google product help forum which can be found here
I found the best option to create separate helper methods for each individual button, which then pass the correct parameter to the generic method.
In the below example you would bind to the buttons the city specific method, for example for "Helsinki" button "updateAvailabilityInfoHelsinki".
function updateAvailabilityInfoHelsinki() {
updateAvailabilityInfo("Helsinki");
}
function updateAvailabilityInfoPorvoo() {
updateAvailabilityInfo("Porvoo");
}
function updateAvailabilityInfo(citySheet) {
// Do something with citySheet parameter
}

How to select value from a dropdown using JScript (not JavaScript) with TestComplete

Currently I'm working on TestComplete automation tool. I'm facing a problem in selecting a value from a dropdown using Jscript. It can done easily in javascript by
document.getElementById("id").options[1].selected=true
I cant do it using JScript'. I've tried
Browsers.Item(btIExplorer).Run("Page URL"); //opening the browser and running a URL
browser=Aliases.browser; //creating alias of browser object
page=browser.Page("PageURL"); //creating a page object for the page opened
page.NativeWebObject.Find("id", "defaultLocationBinder_newOrExisting", "input") // This is the dropdown
I'm not able to find any suitable option to select the options in the dropdown which are given in the <option></option> tags
I just wrote this piece of code and was able to do it.
Use the selectedIndex to set the option you want.
use the object spy to check the properties/methods you can use with the object.
function loginDropDown()
{
var dropDown = Sys.Browser("iexplore").Page("*").FindChild("Name","Select(\"myList\")",10,true)
dropDown.selectedIndex = 1
}
The NativeWebObject.Find method returns a native object while you may want to work with a TestComplete wrapper. Use the Find or FindChild method to get such a wrapper and the Clickitem method to select a specific item.
function test()
{
var b = Sys.Browser("iexplore");
b.ToUrl("http://support.smartbear.com/message/?prod=TestComplete");
var page = b.Page("http://support.smartbear.com/message/?prod=TestComplete");
var cBox = page.FindChild("ObjectIdentifier", "ddlRequestType", 20);
cBox.ClickItem("General product question");
}

Resources