3

Write Widget names to a text box

 4 months ago
source link: https://community.sap.com/t5/technology-q-a/write-widget-names-to-a-text-box/qaq-p/12517784
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Solved: Write Widget names to a text box

Write Widget names to a text box

JBARLOW

Contributor

on

‎01-31-2022

Hi all,

In the same vein as this question: https://answers.sap.com/questions/13572142/within-script-access-widgets-dynamically.html

How would I populate a text array with the names (ID's) of widgets?The aim being to then add the array values to a text box in an app (this bit I think I can do)

ScenarioI have 4 charts in my app: Chart_1, Chart_2, Chart_3, Chart_4

Using the code below I can write the Chart names to the console

var WIDGETS = Application.getWidgets({type:WidgetType.Chart});
console.log(WIDGETS);

The output looks like this - i'm just not sure how to extract the id value and write that to an array 😞

2015546-charts.png

Any ideas much appreciated,

Thanks

Accepted Solutions (1)

SJohnson

Employee

‎02-16-2022

Hi James,
I took a look at what you're trying to do and it doesn't seem possible natively within SAC right now. Unfortunately it has pretty strict rules around casting which prevents us from casting the object to a String and the ID/Name property is not visible as part of the returned object.

To get around this limitation I have built a very simple custom widget that takes in the array of Widgets returned by the getWidgets() call and returns an array of strings representing the widget names.

Right now I can't find the Type Library for the WidgetType class so I had to use the explicit type class (e.g. Chart, Table, etc.)

Anyway, the code required to populate a TextArea would look similar to below

var WIDGETS = Application.getWidgets({type:WidgetType.Chart});
var names = GetWidgetName_1.getChartNames(WIDGETS);
for(var i=0;i<names.length;i++){
	var currText = TextArea_1.getValue();
	TextArea_1.setValue(currText + names[i]+"\n");
}

Below is a screenshot of the Chart names appearing in the Text Area

2022693-screenshot-2022-02-16-133749.png

Of course you can perform any other action against the array (e.g. indexOf, length, etc.). The index values will match the index values of the getWidgets() method so you can use the indexOf to search by widget name and then use the resulting value as the index of the getWidgets array.

I have published the JS file to our SAP BTP tenant, so if you want to give it a test then all you should need to do is publish the Custom Widget to your SAC tenant. If you haven't published a Custom Widget it's very straightforward. You simply need to upload the attached JSON file from the Custom Widget interface (as seen below):

2022694-screenshot-2022-02-16-140234.png

Once you have done this you should be able to access the Custom Widget from within an Analytic Application.

If you want to modify the JS code I have attached this as well. Right now it simply has a method for each of the supported Widget Types to return the Name but perhaps you would like to do something else.

Hope this works for you.

Best Regards,
Sean

The links to the files are below. I was not allowed to upload them with their native extensions so I have attached a ",txt" at the end of each name. You will need to revert the name back to the JSON and JS extensions for these to work.

getwidgetnamejson.txt

getwidgetnamejs.txt


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK