Type Casting in Apps Script - javascript

First time on StackOverFlow!
I am working on building an app that takes in a value through a UI Form TextBox. Once that form is submitted it calls a method that then appends a "/" to the value. The problem is that .append() is not available, because getElementById() returns a GenericWidget object and thus cannot be operated on as if it were a string. I have tried type casting it use .toString() in the var userinput = app.getElementById('input').toString; call and afterward using userinput = userinput.toString.
I have been working with Apps Script for about a month and a few days now and I think that casting to a different type other than GenericWidget would be helpful for anyone who wants to modify a value in a type specific way after passing the value to another method.
I have also done a good bit of research trying to find a solutiong for my problem but like a couple times in the past working with Apps Script I find that since it is a younger language there isn't as much helpful information as there is with languages like Javascript, HTML, and XML. Any help is appreciated.

You have to get the textBox value using e.parameter.textBoxName an then re-assign a value to the textBox. A short example will be more explicit
function doGet(){
var app = UiApp.createApplication();
var textbox = app.createTextBox().setName('txt').setId('txt')
// add other elements, handlers, callBackElements, etc...
}
function changetext(e){
var app = UiApp.getActiveApplication();
var textBoxValue = e.parameter.txt ; // get the value in the text box
var txtBoxWidget = app.getElementById('txt') ; get the widget by its ID
txtBoxWidget.setText(textBoxValue+'/'):// assign the modified value
return app ;// update the UI with new value
}

Related

Creating Datavalidation with custom Formula

I want to create DataValidation for a cell in google sheets with google app scripts, but i cannot find the syntax to create a custum formula for the validation.
My idea is to create a code to validate a time format HH:MM. For this matter i already have a a working Regexp (function CheckRegexp)
The only documentation i´ve found so far to this issue is this one: https://developers.google.com/apps-script/reference/spreadsheet/data-validation-criteria
function test() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange("E4");
var criteria = SpreadsheetApp.DataValidationCriteria.CUSTOM_FORMULA
//Custom formula CheckRegexp
var dv = SpreadsheetApp.newDataValidation().withCriteria(criteria, args).build();
cell.setDataValidation(dv);
}
function CheckRegexp(input) {
return /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/.test(input);
}
I want the result to be a Data Validation for my Regexp in a desire range.
It's not possible to set a custom formula(script-based) as a data validation criteria. Custom formula in this context(DataValidationCriteria.CUSTOM_FORMULA) refers to custom crafted formula by nesting inbuilt formulas.
Possible solution(s):
Use a edit trigger onEdit(e) to check each edit, whether it satifies a condition and clear the cell e.range.clear(), if not OR
Use inbuilt formula like this:
=REGEXMATCH(TO_TEXT(E4),"^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$")
To Read:
Event Objects
REGEXMATCH
Just another way for data validation is below:
=AND(ISDATE(E4), TEXT(E4, "hh:mm")=TO_TEXT(E4))
REGEXMATCH function is also suitable, but it may appear to be less readable for somebody.
By the way, both cases do not reject incorrect numerical input (I dont know why). They only mark cells as "Invalid: This cell's contents violate its validation rule".
It's too late, but might help someone still looking for it. Here's how you can set Custom Formula validation through script:
var range = SpreadsheetApp.getActive().getRange("A2");
var criteria = SpreadsheetApp.DataValidationCriteria.CUSTOM_FORMULA;
var args = ["=ISNUMBER(A2)"];
var val = SpreadsheetApp.newDataValidation().withCriteria(criteria,args);
val.setAllowInvalid(false);
val.setHelpText("Please enter valid number.");
range.setDataValidation(val);

addition in javascript is resulting in an object

I am tring to add 2 values together in a javascript application however when I try and do this the output I get is;
Total Price £[object Object]116.96
Where [object Object] is the value I am trying to add to 116.96
the code I am using to do the addition is bellow;
document.getElementById("getTotal").addEventListener("click", function()
{
var STotal = (($('#SeatPrice')+(subTotal)).toString());
$('#total').text(STotal);
});
these are where the values for '#seatPrice' and '#total' are derived from
$('#total').text(subTotal.toString());
$('#SeatPrice').text((($('td.selected').length)+count)*pricing);
If anyone has any ideas on how to resolve this issue please let me know.
Thanks!
The problem lies here: var STotal = (($('#SeatPrice')+(subTotal)).toString());
$('#SeatPrice') is a jquery function which returns an object - the html element you've searched for. So when you add that to your subTotal, you are adding the object to a string which, in javascript, will just create a string out of both.
You probably want to get the value of that element using something like $('#SeatPrice').val() or $('#SeatPrice').text()

crm 2011 xrm.page.getattribute returns null when there is value

I'm not really skillful with JScript but I've written the following code within a function webResource that is supposed to run when the form loads (OnLoad)
var creationDateName = "createdon";
var today = new Date();
var creationDateAttr = Xrm.Page.getAttribute(creationDateName);
if(creationDateAttr!=null)
{
/* Some more code */
}
The problem is that Xrm.Page.getAttribute(creationDateName) returns null when there is a value on the field (the field is not being displayed in form but when looking up it has a value). Funky thing about it is that in my test organization the same code worked like a charm, but when I copied and paste it on the production organization to release the solution it doesn't work properly that's what confuses me a lot. Any idea what might be happening?
You need to use getValue to return the value in the field. Your code is merely checking that the field exists on the Page.
You also need to be aware that in Create mode, these values are not set, so you can't retrieve them. In Update mode they will work. So you need to check that the Page is in Update mode too:
var formType = Xrm.Page.ui.getFormType();
if (formType === 2) // Update
{
var creationDate = Xrm.Page.getAttribute(creationDateName).getValue();
}
it gives you the attribute not the value..
to get the value you have to write code as below
var creationDateAttr = Xrm.Page.getAttribute(creationDateName);
var valueDateAttr=creationDateAttr.getValue();
OR
var creationDateAttrValue = Xrm.Page.getAttribute(creationDateName).getValue();
hope this will help
Silly me, I forgot to add the field I was looking for that's why it return null but thanks to Donal for the answer, actually I was trying to verify if the field was full or was null. Thanks

Need to get an array of the names of all applicationScope variables

In an application I am working on I need to get a list of the names of all applicationScope variable then I need to cycle through them and filter out the ones starting with a know string say $xyx. I thought that the applicationScope.keySet().
I'm using this code for starter:
var col = applicationScope.keySet();
var itr:java.util.Iterator = col.iterator();
if (itr.hasNext()){
var str:String = itr.next();
dBar.info(str,"Value = ");
}
if I put the variable col in a viewScope it shows a list of all the keys. but when I run the script the values displayed in the dBar info are not the keys but some other information that I'm not sure where it comes from.
I should just be able to iterat through the list of keys, am I missing something?
This code is in the before page loads event
After some poking around and experimenting I got this to work:
var col = applicationScope.keySet();
var itr:java.util.Iterator = col.iterator();
while (itr.hasNext()){
var str:Map.Entry = itr.next();
if (str.substring(0,9) == "$wfsLock_"){
//do stuff
}
}
so I'm now a happy camper.
Although your code works in SSJS, it is not correct (and that's why I don't like SSJS...).
The applicationScope is an implementation of the java.util.Map interface and the keySet() method returns a Set containing the keys in that Map. Every entry is (probably) a String (other data types like integers are actually also valid). The line
var str:Map.Entry = itr.next();
doesn't cast it to a Map.Entry: it doesn't really do anything: str remains a string.
The Map interface also has an entrySet() method that returns the entries (Map.Entry). You can use that to retrieve the key as well as the value:
var it = applicationScope.entrySet().iterator();
while (it.hasNext()) {
var entry = it.next();
print( entry.getKey() + " = " + entry.getValue() );
}
(in this code the print() line will use the toString() method of the key as well as the value to send information to the console)
I see from your code that you've installed my XPages Debug Toolbar. You can also use that to quickly check what's in the scopes and what the actual datatype is.

Process hash fragment using JavaScript

i want to get hash parameters value in my java script
for example my url should be like that
www.example.com/#!mydata=data&myopt=option
i want to get mydata in variable which will be "data" as value,
and myopt "option"
iam trying to implement google ajax cowling like here
Google Code
and i have tried to implement jquery address
but with big fail so help me either by solving 1st part or give me simple walk through tutorial to implement jquery address to my ajax requests ,thank you
This piece of code will convert any well formed (i.e. properly url-encoded) request string into an object literal with values parsed.
var s = "#!mydata=data&myopt=option";
var o = {};
$.each(s.substr(2).split('&'), function(i, elem) {
var parts = elem.split('=');
o[parts[0]] = parts[1];
});
Then you can access values like o.myopt
UPDATE
Of course, to get the value from browser's address, you should use
var s = window.location.hash;

Categories

Resources