filling forms, get input name with getElementInfo - casperjs

I try to get dinamicaly the name of the fields and then it fails. It works if I write the name between quotes.
var elms = this.getElementsInfo('select');
var name1 = elms[0].attributes.name;
var name2 = elms[1].attributes.name;
utils.dump(name1); // "tb|2564"
utils.dump(name2); // "tb|19"
This works:
this.fill('form#tbsa', {
"tb|2564": 10,
"tb|19": 15
}, true);
This fails:
this.fill('form#tbsa', {
name1: 10,
name2: 15
}, true);
Anybody knows why?
Thanks,
- Albin

You can't do it in the way you do at the moment, because this will set the values of the properties name1 and name2.
If you want to to it by the parameter names in a variable you have to create the object first.
This would be following code:
var foo = {};
foo[name1] = 10;
foo[name2] = 15;
this.fill('form#tbsa', foo, true);

Related

How to make this script run in all tabs except certain tabs (Sheets)?

I'm a total newbie when it comes to scripts and I honestly don't really use it often at all but I thought it'd be fun to automatize an alphabetical order sorting I wanted to do and so I used this script:
/** Build a menu item
From https://developers.google.com/apps-script/guides/menus#menus_for_add-ons_in_google_docs_or_sheets
**/
function onOpen(e) {
var menu = SpreadsheetApp.getUi().createMenu('Sort');
if (e && e.authMode == ScriptApp.AuthMode.NONE) {
// Add a normal menu item (works in all authorization modes).
menu.addItem('Sort Sheet', 'sort');
} else {
// Add a menu item based on properties (doesn't work in AuthMode.NONE).
var properties = PropertiesService.getDocumentProperties();
var workflowStarted = properties.getProperty('workflowStarted');
if (workflowStarted) {
menu.addItem('Sort Sheet', 'sort');
} else {
menu.addItem('Sort Sheet', 'sort');
}
menu.addToUi();
}
}
function sort() {
/** Variables for customization:
Each column to sort takes two variables:
1) the column index (i.e. column A has a colum index of 1
2) Sort Asecnding -- default is to sort ascending. Set to false to sort descending
**/
//Variable for column to sort first
var sortFirst = 3; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
var sortFirstAsc = true; //Set to false to sort descending
//Variables for column to sort second
var sortSecond = 1;
var sortSecondAsc = true;
//Number of header rows
var headerRows = 1;
/** End Variables for customization**/
/** Begin sorting function **/
var activeSheet = SpreadsheetApp.getActiveSheet();
var sheetName = activeSheet.getSheetName(); //name of sheet to be sorted
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn());
range.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}]);
}
It worked very well but I wondered if there was a way to not have it work in two specific tabs of the same sheets?
I believe your goal as follows.
You want to execute the script of sort for the sheets except for the specific sheets.
In this case, how about declaring the excluded sheet names and checking the current sheet using the excluded sheet names? When this is reflected to your script, it becomes as follows.
Modified script:
In this case, please set the sheet names you want to exclude to excludeSheetNames. At sample script, when the active sheet is "Sheet1" and "Sheet2", the script below the if statement is not run.
function sort() {
var excludeSheetNames = ["Sheet1", "Sheet2"]; // <--- Added
var sortFirst = 3;
var sortFirstAsc = true;
var sortSecond = 1;
var sortSecondAsc = true;
var headerRows = 1;
var activeSheet = SpreadsheetApp.getActiveSheet();
var sheetName = activeSheet.getSheetName();
if (excludeSheetNames.includes(sheetName)) return; // <--- Added
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var range = sheet.getRange(headerRows + 1, 1, sheet.getMaxRows() - headerRows, sheet.getLastColumn());
range.sort([{ column: sortFirst, ascending: sortFirstAsc }, { column: sortSecond, ascending: sortSecondAsc }]);
}
For example, if you want to run the script below the if statement for only excludeSheetNames, please modify if (excludeSheetNames.includes(sheetName)) return; to if (!excludeSheetNames.includes(sheetName)) return;.
Reference:
includes()

Nativescript RadDataForm - Provide valuesProvider from code behind

I have a form which needs to generate its fields dynamically. The fields will be of two type: text or dropdown. The text part is working fine but I'm having issues with the dropdown. I can't seem to set the valuesProvider of the field. Here's the code:
const DataFormEditorType = require('nativescript-ui-dataform').DataFormEditorType;
exports.onLoaded = function(args) {
var page = args.object;
var productVM = Observable({
id: 1
});
page.bindingContext = productVM;
dataForm = page.getViewById("myDataForm");
let configForm = {};
var name = 'Dropdown'; //loaded dynamically
configForm[name] = ''; //add field to list of fields with empty initial value
productVM.set('config', configForm);
console.log(dataForm); //return RadDataForm
var temp = dataForm.getPropertyByName('Dropdown');
console.log(temp); //returns null
var pickerEditor = new PropertyEditor();
pickerEditor.type = DataFormEditorType.Picker;
temp.editor = pickerEditor;
temp.valuesProvider = 'Test 1, Test 2'; //will be loaded dynamically
}
XML
<df:RadDataForm id="myDataForm" source="{{ config }}" />
I'm trying to follow this example but I guess it doesn't translate into JS exactly.
Edit:
I've managed to get to the property. The problem was spaces in the names of fields. After converting all names to camelcase, I', getting a value in temp variable but still no success on the valuesProvider. I've tried
temp.valuesProvider = 'Test 1, Test 2';
temp.valuesProvider = ['Test 1, Test 2'];
temp.valuesProvider = ['Test 1', 'Test 2'];
None of these work.

JSLINQ - Distinct from multiple columns

I have a working JSFiddle demo
https://jsfiddle.net/u1fohjxw/
The idea is to create a unique list of items, based on multiple fields.
I know the way I've implemented it could be improved, but need advice on doing it in JSLINQ.
This filtered unique list I then manually loop and add again - this could be done in JSLINQ.
Please indicate how this should be done :
var myList = [
{FirstName:"Chris",LastName:"Pearson"},
{FirstName:"Chris",LastName:"Pearson"},
{FirstName:"Chris",LastName:"Sutherland"},
{FirstName:"John",LastName:"Ronald"},
{FirstName:"Steve",LastName:"Pinkerton"}
];
var exampleArray = JSLINQ(myList)
.Distinct(function(item){ return item.FirstName.concat(";",item.LastName)}).items
var newList = [];
for (var x = 0 ; x < exampleArray.length ; x++) {
var arraylist = exampleArray[x].split(";");
var y= new Object();
y.FirstName = arraylist[0];
y.LastName = arraylist[1];
newList.push(y);
};
how you doing? :)
Maybe something like this helps you out:
var myList = [
{FirstName:"Chris",LastName:"Pearson"},
{FirstName:"Chris",LastName:"Pearson"},
{FirstName:"Chris",LastName:"Sutherland"},
{FirstName:"John",LastName:"Ronald"},
{FirstName:"Steve",LastName:"Pinkerton"}
];
var resultList = myList.Distinct(function(x){
return {
FirstName: x.FirstName,
LastName: x.LastName
}
}).ToArray();
This will return an array of the object returned inside the distinct.
Edit:
Change the distinct method to this:
Distinct: function(clause) {
var item, dict = {}, retVal = [];
for (var i = 0; i < this.items.length; i++) {
item = clause.apply(this.items[i], [this.items[i]]);
if (dict[JSON.stringify(item)] === undefined) {
dict[JSON.stringify(item)] = true;
retVal.push(item);
}
}
dict = null;
return JSLINQ(retVal);
},
It's not stress tested, I don't know how much time will take to iterate through 10k+ objects, but it's something to study and improve! :)
There's another possible fix to this if you want to try.
Cheers!

Dictionaries not working inside a struct SWIFT

Why is this not working!?
struct ChocolateBox {
var caramelDelight = []
caramelDelight["flavor"] = "caramel"
}
I tried this without the struct, still doesn't work:
var caramelDelight = []
caramelDelight["flavor"] = "caramel"
I have to add initial values into the array for it to work, for example:
var caramelDelight = ["test":"test"]
caramelDelight["flavor"] = "caramel"
Please explain.
Your var caramelDelight = [] doesn't create an empty dictionary.
To create an empty dictionary use [:]() and specify the types of the keys and values, example: var caramelDelight = [String:String]().
There's also this alternative syntax: var caramelDelight: [String:String] = [:].
Also to modify the var in your struct you need to create an instance of the struct first:
struct ChocolateBox {
var caramelDelight = [String:String]()
}
var cb = ChocolateBox()
cb.caramelDelight["flavor"] = "caramel"
println(cb.caramelDelight) // [flavor: caramel]
UPDATE:
You can also create an initializer for your struct if you need to prepopulate the dictionary:
struct ChocolateBox {
var caramelDelight: [String:String]
init(dict: [String:String]) {
self.caramelDelight = dict
}
}
var cb = ChocolateBox(dict: ["flavor": "caramel"])
Of course then you can update the dictionary as usual:
cb.caramelDelight["color"] = "brown"
println(cb.caramelDelight) // [color: brown, flavor: caramel]
That is because caramelDelight is actually an array, not a dictionary. You can fix that by doing var caramelDelight: [String:String] = [:]

Union 2 lists with additional content change for ID that occurs in both source lists

How to merge these 2 collections?
Problem: When an ApplicationID is in listOfPM and listOfPM2, Test2 is null and should be a number
var listOfPM = from d in gbc
select new Tab24PresentationModel
{
ApplicationID = d.ApplicationID,
ApplicationName = d.ApplicationName,
Test1 = d.count,
Test2 = null
};
var listOfPM2 = from d in gbc2
select new Tab24PresentationModel
{
ApplicationID = d.ApplicationID,
ApplicationName = d.ApplicationName,
Test1 = null,
Test2 = d.count
};
var result = listOfPM.Union(listOfPM2);
Have tried removing the Test2 from list of PM and Test1 from listOfPM2 from each and get:
The type 'xxx.Tab24PresentationModel' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
I can think of ways around this using multiple foreach's.. would like to use Linq!
You need to merge the two objects (with same ApplicationID) manually, no way around that.
Edit - try this:
var list = gbc.Union( gbc2 ).ToLookup( d => d.ApplicationID ).Select( g =>
{
var first = g.First();
var retVal = new Tab24PresentationModel
{
ApplicationID = first.ApplicationID,
ApplicationName = first.ApplicationName,
Test1 = first.count,
Test2 = null
};
var second = g.Skip(1).Take(1).SingleOrDefault();
if( null != second )
{
retVal.Test2 = second.count;
}
return retVal;
} );
Edit 2: hrm, that would only work if you don't want Test1 = null, Test2 = value when only the gbc2 exists. If that's not an issue, you should be ok.
Many thanks for the answers and comments - here is what I came up with.
// take each list in turn and either add or update
var result = new List<Tab24PresentationModel>();
foreach (var t in listOfPM)
{
var a = new Tab24PresentationModel
{
ApplicationID = t.ApplicationID,
ApplicationName = t.ApplicationName,
Test1 = t.Test1
};
result.Add(a);
}
// list2
foreach (var t in listOfPM2)
{
// is this already in result
if (result.Any(x => x.ApplicationID == t.ApplicationID))
{
var existing = result.First(x => x.ApplicationID == t.ApplicationID);
existing.Test2 = t.Test2;
}
else
{
var a = new Tab24PresentationModel
{
ApplicationID = t.ApplicationID,
ApplicationName = t.ApplicationName,
Test2 = t.Test2
};
result.Add(a);
}
}

Resources