Delete from array with reindex - AS3.0 - actionscript

I would like to delete a item from my array and still have the same index for every item afterwards.
Example
var arr:Array = new Array();
arr[1] = 'One';
arr[2] = 'Two';
arr[3] = 'Three';
arr.splice(2, 1);
for(var index in arr) {
trace(index+':'+arr[index]);
}
Outputs:
1:One
2:Three
And should ouput
1:One
3:Three
Anyone that could help me a little bit here? :-)

If you splice an element from an array then the array is refreshed and the elements after that index is moved to fill that index.
If you really want to have the elements in the same index always then do assign the index with empty string instead of splicing it.
Try this:
array[1] = "";
instead of,
array.splice(1, 1);
According to me splicing is a good way of practicing.

You can use delete operator:
var arr:Array = new Array();
arr[1] = 'One';
arr[2] = 'Two';
arr[3] = 'Three';
delete arr[2];
for(var index in arr) {
trace(index+':'+arr[index]);
}

I found a solution to my problem.
Instead of using a array, i'll use a object to hold my items. Like this
var objArr = {1: 'One', 2: 'Two', 3: 'Three'};
And when I delete i do like this.
delete objArr[1];
I know I wont get the array functions then. But in my case I don't need them.

Related

The names of sheets are changed when users use different languages

I use the formula {'Form Responses 1'! A1: G; 'Form Responses 2'! A2: G; ...} to get data from all sheets into one sheet. However, when the user uses a language other than English, the sheets' names are changed, and the related formulas are corrupted.
Can anyone help me to solve this problem, thank you in advance.
The code:
function copy() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SpssId = ss.getSheetByName('title').getRange('AH6').getValue();
var destinationSpss = SpreadsheetApp.openById(SpssId);
for (var i = 1; i < destinationSpss.getNumSheets(); i++) {
var index = destinationSpss.getSheetByName('index');
index.getRange(i+1,2,1,4)
.setFormulas([["='Form Responses "+i+"'!B2","='Form Responses "+i+"'!D2","='Form Responses "+i+"'!C2","='Form Responses "+i+"'!E2"]]);
}}
Thanks for providing the code. It was very helpful to see what you had so far.
Can we assume that index is the only sheet in the destination spreadsheet that is not one of the form response sheets? Judging from your code, it seems like we can assume this.
So then we can just use Spreadsheet.getSheets() to get an array of all the sheets and Sheet.getName() to get their names, regardless of language:
function copy() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SpssId = ss.getSheetByName('title').getRange('AH6').getValue();
var destinationSpss = SpreadsheetApp.openById(SpssId);
var sheets = destinationSpss.getSheets() // Get an array of the sheets
var index = destinationSpss.getSheetByName('index');
for (var i = 0; i < sheets.length; i++) {
var sheetName = sheets[i].getName();
if (sheetName === 'index') break;
index.getRange(i + 1, 2, 1, 4)
.setFormulas([[`="${sheetName}"!B2`,`="${sheetName}"!D2`,`="${sheetName}"!C2`,`="${sheetName}"!E2`]])
}
}
(I'm using template literals to format the formula strings, which just makes the code a little cleaner, but this is not necessary of course.)
Can you always assume that the index sheet is at index 0? I'm not sure if you can, so I would expand your for-loop to include i = 0 and add the check if (sheetName === 'index') break; to break if you're on the index sheet.
As an aside, you'll see I also moved var index = destinationSpss.getSheetByName('index'); outside of the for-loop. The App Scripts API can be slow for certain operations, and since you're referencing the same index sheet for every iteration, your code will be faster if you call for this index sheet just once.

Concatenate certain rows into a cell ignoring duplicates

I have a google form and I would like to sort it's responses in a separate sheet on google sheets. The results of the form look sort of like this.
Id Job
1 Shelving, Sorting
2 Sorting
1 Cleaning, Shelving
3 Customer Service
2 Shelving, Sorting
which I would like to format into
Id Jobs
1 Cleaning, Shelving, Sorting
2 Shelving, Sorting
3 Customer Service
Is there a formula I can use to accomplish this, noting that it ignores duplicates and groups the different ids? Ordering of the jobs does not matter.
Working example here.
The code is like:
=unique(transpose(split(join(", ",filter(B1:B10,A1:A10=1)),", ")))
where
filter(B1:B10,A1:A10=1) gives you all the B values where A = 1
join(",", filter(...)) joins the list with the ", " separator (e.g. "apple, orange" and "kiwi" becomes "apple, orange, kiwi"
split(join(...)) splits the list into an array (e.g. back to [apple, orange, kiwi]
transpose(split(...)) converts the horizontal list to vertical list
unique(transpose(...)) gives you the unique values (unique() only works with vertical list)
After this, you need to transpose then join the list
Note you must keep the separator consistent (e.g. always "," or ", ")
This is Apps Script code instead of a function. To use it, you will need to use the Tools menu, and open the script editor. Then select the function name from the drop down list, and then click the "Run" button.
To use this code, you need to have a source and a destination sheet. You will need to change the sheet names in the code to your sheet names. In this code, the source sheet is named 'Data'. You will need to change that to the name of your source sheet. In this code, the destination sheet is named 'Output', and is at the bottom of the code. This code gets data starting in row two, and writes the output data starting in row two. I tested it with your values and it works.
function concatCellData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('Data');
var colOneData = sh.getRange(2, 1, sh.getLastRow()-1, 1).getValues();
var colTwoData = sh.getRange(2, 2, sh.getLastRow()-1, 1).getValues();
var newData = [],
newDataColOne = [],
colOneValue,
existInNewData = false,
colB_One,
colB_Two,
dataPosition,
thisValue,
combinedArrays = [],
arrayTemp = [];
for (var i=0;i<colOneData.length;i+=1) {
colOneValue = colOneData[i][0];
dataPosition = newDataColOne.indexOf(colOneValue);
existInNewData = dataPosition !== -1;
if (!existInNewData) {//If it doesn't exist in the new data, just write the values
newDataColOne.push(colOneValue);
newData.push([colOneValue, colTwoData[i][0]]);
continue;
};
colB_One = [];
colB_Two = [];
combinedArrays = []
arrayTemp = [];
colB_One = colTwoData[i][0].split(",");
colB_Two = newData[dataPosition][1];
colB_Two = colB_Two.split(",");
var combinedArrays = colB_One.concat(colB_Two);
//Get unique values
for (var j=0;j<combinedArrays.length;j+=1) {
thisValue = combinedArrays[j].trim();
if (arrayTemp.indexOf(thisValue) === -1) {
arrayTemp.push(thisValue);
};
};
newData[dataPosition] = [colOneValue, arrayTemp.toString()]; //Overwrite existing data
};
ss.getSheetByName('Output').getRange(2, 1, newData.length, newData[0].length).setValues(newData);
};

Dustjs Display Unique Values Only?

Lets say i have the following JSON
{
names: ["John", "Peter", "Ron", "John", "James", "John"]
}
I need DustJS to render the following names
John
Peter
Ron
James
Notice that these are unique values in an array. Any ideas? Thank you so much!
This can be done using a common algorithm to 'unique' an array:
Array.prototype.getUnique = function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
It's done by taking the values, attempting to add them as keys to an object (which will only work if they're different). If success, it adds that key to an array. If fail, it ignores the key. It then returns the array. I have a working dust.js demo here:
Working Demo
I believe this will generate your "also acceptable" form:
{#options}] {.} {#variants} {.options[$idx]} {/variants} {/options}

Is there any way get datarow from datatable without using foreach

Controller
foreach (DataRow temp in act.Rows)
{
_oResolutionModel.activityNo = temp["ActivityID"].ToString();
_oResolutionModel.assignTechnician = temp["TechNo"].ToString();
_oResolutionModel.recommendation = temp["RECOMMENDATION"].ToString();
_oResolutionModel.jobStart = (DateTime)temp["JobStart"];
_oResolutionModel.jobEnd = (DateTime)temp["JobEnd"];
_oResolutionFacade.setResolutionID(_oResolutionModel.activityNo);
DataTable res = _oResolutionFacade.getResolution(_oAppSetting.ConnectionString);
foreach (DataRow x in res.Rows)
{
_oResolutionModel.solution = x["Resolution"].ToString();
_oResolutionModel.remarks = x["Remarks"].ToString();
_oResolutionList.Add(_oResolutionModel);
break;
}
_oResolutionList.Add(_oResolutionModel);
break;
}
In here my _oResolutionList count = 1, meaning there's two data in it and it duplicated the first data. I want to have only 1 data in my _oResolutionList. Do I need to add some code in my inner Foreach or should I change something on it.?
Or You can suggest me how to delete the second data entry.?
Instead of using a foreach loop. You can also check the nullity first and then assign.
You can also do :
_oResolutionFacade.setResolutionID(_oResolutionModel.activityNo);
DataTable res = _oResolutionFacade.getResolution(_oAppSetting.ConnectionString);
_oResolutionModel.solution = res.Rows[0]["Resolution"].ToString() ?? string.Empty; //To make sure that if it is null it will assign to an empty string
_oResolutionModel.remarks = res.Rows[0]["Remarks"].ToString()?? string.Empty;
You have many solutions to deal with that.
Hope it will help you

LINQ Union Allways returning empty sets

I'm doing some LINQ to look for keywords typed in a textbox. Each keyword ends with ";" and i need to look for itens that contain at least one of those keys.
I thought that i would be able to achieve this with this loop
IEnumerable<ItemFAQ> allResults = null;
foreach (var item in termosPesquisaIsolados)
{
IEnumerable<ItemFAQ> tempResults =
_dbHelpDesk.ItemFAQ
.Where(x => x.DescricaoResposta.Contains(item) || x.TituloTopico.Contains(item))
.ToList()
.Select(x => new ItemFAQ
{
IdPergunta = x.IdPergunta,
TituloTopico = x.TituloTopico,
DescricaoResposta = x.DescricaoResposta.Substring(0, 100)
});
if (allResults != null)
allResults = allResults.Union(tempResults);
else
allResults = tempResults;
}
At first iteration tempResult returns in a test 3 elements then it passes then to allResult, everything ok with that.
The second iteration, tempResult returns 2 ocurrences... then according to code allResult should receive the Union of AllResult and the TempResults, but no matter what i do, when i use the Union clause the result in an Empty Set.
So 3 + 2 = 0 at the end after using Union.
Do you guys can see the mistake at this peace of code or know some issues that could lead to this error ?
Thanks for your time.
try replacing this line:
allResults = allResults.Union(tempResults);
with:
allResults = allResults.Union(tempResults).ToList();

Resources