I am wanting to perform the concatenation as shown below in the objConcat variable but for X number of objects. Sometimes I'll need to concatenate two objects, and sometimes I'll need to concatenate 50 objects.
How can I iterate this in a For loop where I don't have to manually create these individual object variables?
The importData() function returns an object.
var obj1 = importData(0);
var obj2 = importData(1);
var obj3 = importData(2);
var objConcat = obj1.concat(obj2).concat(obj3);
You could get the first data and save it in a variable. Then, loop through the rest and concatenate each of it to the initial variable.
var objConcat = importData(0);
var n = 50;
for (var i=1; i<n; i++) {
objConcat = objConcat.concat(importData(i));
}
Related
I have two spreadsheets:
Column A on sheet 6th&7thRoster lists all IDs in a sample, contains 853 items.
Column C on sheet alreadySubmitted contains the IDs of users who've completed a task. Contains 632 items.
I'm trying to parse through both columns. If a user from Column A of sheet 6th&7thRoster matches a user from Column C of sheet sandboxAlreadySubmitted, I want to write the word "Yes" on Column I of the current row of sheet 6th&7thRoster. When using the code below, I'm not seeing not seeing any instances of the word "Yes" on Column I of 6th&7thRoster, even though I know there's multiple places where that should be the case.
function checkRoster() {
var mainSheet = SpreadsheetApp.openById('XXXXXXX');
var roster = mainSheet.getSheetByName('6th&7thRoster');
var submissions = mainSheet.getSheetByName('alreadySubmitted');
var rosterLastRow = roster.getLastRow();
var submissionsLastRow = submissions.getLastRow();
var rosterArray = roster.getRange('A2:A853').getValues();
var submissionsArray = submissions.getRange('C2:C632').getValues;
var i;
var x;
for (i = 1; i < 853; i++) {
for (x = 1; x < 632; x++){
if (rosterArray[i] == submissionsArray[x]){
roster.getRange(i, 9).setValue("Yes");
}
}
}
}
Feedback on how to solve and achieve this task will be much appreciated. For confidentiality, I cannot share the original sheets.
You want to compate the values of A2:A853 of 6th&7thRoster and C2:C632 of alreadySubmitted.
When the values of C2:C632 of alreadySubmitted are the same with the values of A2:A853 of 6th&7thRoster, you want to put Yes to the column "I".
If my understanding is correct, how about this modification? Please think of this as just one of several possible answers.
Modified script:
function checkRoster() {
var mainSheet = SpreadsheetApp.openById('XXXXXXX');
var roster = mainSheet.getSheetByName('6th&7thRoster');
var submissions = mainSheet.getSheetByName('alreadySubmitted');
var rosterLastRow = roster.getLastRow();
var submissionsLastRow = submissions.getLastRow();
var rosterArray = roster.getRange('A2:A853').getValues();
var submissionsArray = submissions.getRange('C2:C632').getValues(); // Modified
// I modified below script.
var obj = submissionsArray.reduce(function(o, [v]) {
if (v) o[v] = true;
return o;
}, {});
var values = rosterArray.map(function([v]) {return [obj[v] ? "Yes" : ""]});
roster.getRange(2, 9, values.length, values[0].length).setValues(values);
}
Flow:
Retrieve values from A2:A853 of 6th&7thRoster and C2:C632 of alreadySubmitted.
Create an object for searching the values from the values of alreadySubmitted.
Create the row values for putting to 6th&7thRoster.
References:
reduce()
map()
If I misunderstood your question and this was not the direction you want, I apologize.
I need to rotate the match through variables Cat1 to Catx as long as there is data for the Cat'x'. Whenever I do this this, it does not consider as a variable but the literal Cat4 or Cat5 instead of the variable Cat4 & Cat5 when I try to compile the new category label. Such as the following with i increasing until there is no value to the variable searched for .. i.e. Cat57 has nothing assigned.
category = "Cat"+i
This is the portion of my code I believe that needs to be adjusted .. essentially based on the category I am going to assign it a specific column in my spreadsheet (this part hasn't been added yet) .. still stuck on the matching through multiple categories
if(studentmarks && studentmarks.length > 0 && assign.maxPoints > 0){
for (d = 0; d < studentmarks.length; d++){
var marks = studentmarks[d];
if(student.userId == marks.userId){
var ss = SpreadsheetApp.openByUrl(url).getSheetByName(shet);
var re = RegExp(Cat1);
if (assign.title.match(re))
ss.appendRow([category, assign.title, marks.assignedGrade,
assign.maxPoints]);
As we have talked in the comments there is no need to actually use RegEx in this case.
Just define an array and iterate through. Without the need
// Define new array
var Cat = [];
//
// Do stuf to populate Cat array
//
//Access your data
for(var i = 0; i < Cat.length; i++){
var element = Cat[i];
}
I'm trying to concat the selected items from 2 grids in to an array for further processing but I don't want to affect any change in either data source and this is proving problematic as the first data source seems to (after the concat) contain the items I pull from the first ...
var allItems = JSLINQ(grid1.data("kendoGrid").dataSource.data())
.Concat(grid2.data("kendoGrid").dataSource.data())
.ToArray();
the source code for the concat function in JSLINQ is doing this ...
Concat: function (array) {
var arr = array.items || array;
return new JSLINQ(this.items.concat(arr));
}
this.items is from what I can tell the value of "grid1.data("kendoGrid").dataSource.data()"
and i'm trying to build a new array with the items in "grid2.data("kendoGrid").dataSource.data()" which I then intend to filter based on selection criteria.
does anyone have any experience with this / a means to say "I want a copy of the data item from the source that's not connected to the source"?
UPDATE:
The base functionality here relies on having a standard JS array, it seems that kendo returns an observable array object (specific to kendo, and missing the concat function).
The implementation above results in an exception on the concat call (because it doesn't exist), so I rewrote the function to something like this ...
Concat: function (array) {
//var arr = array.items || array;
//return new JSLINQ(this.items.concat(arr));
var retVal = new Array();
for (var i = 0; i < this.items.length; i++) {
var clone = JSON.parse(JSON.stringify(this.items[i]));
retVal.push(clone);
}
for (var i = 0; i < array.length; i++) {
var clone = JSON.parse(JSON.stringify(array[i]));
this.items.push(clone);
}
return new JSLINQ(retVal);
},
That results in the duplicate problem I mentioned above.
So it seems that the error I have here is something to do with observable array, but I don't know how to get a "detatched item" / "array" from the data source.
Ok so it turns out the toJSON() method on an observable array turns the observable array in to an array (odd naming, but hey this is kendo right!)
In short, by manipulating my call input to the JSLINQ method to include this I then have normal JS behaviour as I would expect ...
var allItems = JSLINQ(grid1.data("kendoGrid").dataSource.data().toJSON())
.Concat(grid2.data("kendoGrid").dataSource.data().toJSON())
.ToArray();
How to use Get Set properties on arrays in c# scripting.In order to set particular index and to get the value what is the procedure should be followed?
I am using Test complete software and c#scripting.
TestComplete's C#Script is actually JScript with square bracket notation instead of dot notation. You can manipulate arrays in C#Script in the same way as in JScript (or JavaScript, which is similar):
// Define an array using an array literal
var names = ["Alice", "Bob", "Charlie"];
Log.Message(names[0]); // "Alice"
// Create and populate an array using a loop
var numbers = new Array(); // or: var numbers = [];
for (var i = 0; i < 5; i++) {
numbers[i] = i;
}
Log.Message(numbers.join()); // "0,1,2,3,4"
I have a form where the user submits a query and then have a Servlet that processes this query and returns the results in XML. With this result trying to populate a table dynamically via Ajax, for such, I use the following code below.
var thead = $("<thead>");
var rowsTHead = $("<tr>");
var tbody = $("<tbody>");
var numberOfColumns;
$(xml).find("head").each(function(){
var variable = $(this).find("variable");
numberOfColumns = variable.length;
for (var i = 0; i < variable.length; i++){
var name = $(variable[i]).attr("name");
rowsTHead.append($("<th>").html(name));
}
});
thead.append(rowsTHead);
$(xml).find("result").each(function(){
var literal = $(this).find("literal");
var rowsTBody = $("<tr class=\"even\">");
literal.length = numberOfColumns;
for (var j = 0; j < literal.length; j++){
var tdBody = $("<td>");
tdBody.html($(literal[j]).text());
rowsTBody.append(tdBody);
}
tbody.append(rowsTBody);
});
$(".tablesorter").empty()
.append(thead)
.append(tbody);
This code works perfectly until it was used in a UNION query. When using a UNION the returned xml comes in the following way http://pastebin.com/y7hXK1Zy
As can be observed, this query has 4 variables that are: gn1, indication1, gn2, indication2.
What is going wrong is that the values of all the variables being written in columns corresponding to gn1 and indication1.
What I wish I was to write the value of each variable in its corresponding column. I wonder what should I change in my code to make this possible.
You need to respect the name values of the binding elements, and relate them back to the columns that you correctly built from parsing the element. When you are doing the find "literal", you are skipping the parsing of the binding elements. You should find "binding", respect the name and look up which column to use based on that, and then for each of those, find the "literal" elements for the actual values.