First, I am generating random integer values to get some values to check.
var A1, B2, C3:int;
A1 = Math.random() * 100 + 1;
B2 = Math.random() * 100 + 1;
C3 = Math.random() * 100 + 1;
Then I want to check if all the variables are unique from each other.
if (!(A1 == B2 || A1 == C3 || B2 == C3)){
unique = true;
}else{ // Not unique
}
If the variables are not unique to each other, I want to keep only the value for A1, and then change the two other variables B2 and C3 and then again check if they are unique.
}else{ // Not unique
if (unique = false){
do{
B2 = Math.random() * A1 + 1;
C3 = Math.random() * A1 + 1;
if (!(A1 == B2 || A1 == C3 || B2 == C3)){
unique = true;
}while (unique = true)
}
trace("Not unique");
}
My problem is that I cannot get three unique values, and any help on how I can solve this is highly appreciated.
This is a case where instead of asking to resolve the problem at hand you should probably have started by explaining what you are trying to do and what's the best way to achieve it. Since you have been posting a few times already my guess is that you are trying to extract 3 unique int from a range of 0 to 100. If that's the case then you are trying to create a complex and inefficient way to do it which is interesting by itself but is far from the point. The solution is to simply generate an array of int from 0 to 100 and then extract 3 of them randomly. In that case they will always be unique and always from 0 to 100.
var intRange:Array = [];
for(var i:int = 0; i < 100; i++)
{
intRange.push(i);
}
var index:int = Math.flor(Math.random() * intRange.length);
var a:int = intRange[index];
intRange.splice(index, 1);
index = Math.flor(Math.random() * intRange.length);
var b:int = intRange[index];
intRange.splice(index, 1);
index = Math.flor(Math.random() * intRange.length);
var c:int = intRange[index];
intRange.splice(index, 1);
//3 unique int from 0 to 100 range
Your problem is the wrong code here:
if (unique = false) { // must be: if (unique == false)
do {
...
} while (unique = true) // must be: while(unique == false)
}
also it can be reduced to
while(!unique) {
...
}
Also, there you are taking random from A1, not 100. Just too many errors. The proper code should be:
var a:int, b:int, c:int; // also, why would you call variables A1, B2, C3???
a = Math.random() * 100 + 1;
do {
b = Math.random() * 100 + 1;
while(b == a);
do {
c = Math.random() * 100 + 1;
} while(c == a || c == b);
// here you have unique
Now, the answer given by BotMaster is correct and robust but it involves filling an array and splicing it which is not always ok, especially if random range is significantly more than 100 and we need only three values from it.
Related
I have this form of a spreadsheet:
A B C D
abc abc abc 1
def ghi jkl 1
mno pqr stu 3
vwx yza bcd 4
mno pqr stu 5
mno pqr stu 5
vwx yza bcd 5
mno pqr stu 1
Where the first 3 columns are just data of type string. The column D has integers which has numbers repeating. My question is how to output a fifth column like so:
A B C D E
abc abc abc 1 1
def ghi jkl 1 3
mno pqr stu 3 4
vwx yza bcd 4 5
mno pqr stu 5
mno pqr stu 5
vwx yza bcd 5
mno pqr stu 1
It only outputs the unique numbers from column D.
I imagined running an if/else statement in a for or while loop that checks each cell in "D" and stores any value not previously "seen" in an array. Then outputting the array in column E.
I was wondering if there is a more efficient way to do this. Also the above is just a small example. Most likely the data range is in the 400 range. (Row wise. Columns are only 4 or 5 including the new output column.)
Thanks in advance.
P.S.
I searched for this here but I'm only getting questions that relate to deleting duplicate rows. If there is a question that asks this already, please link me to it.
You can do that inside google-spreadsheets with the UNIQUE function.
Here is the doc to all available functions.
(You find UNIQUE in the Filter category)
Most likely you want to insert into cell E1:
=UNIQUE(D1:D)
This will populate column E with the unique values from all of column D while preserving the order. Furthermore this will dynamically update and reflect all changes made to column D.
To do that from within google-apps-script:
SpreadsheetApp.getActiveSheet()
.getRange("E1").setFormula("=UNIQUE(D1:D)");
here is a way to do that... probably not the only one but probably not bad...
I added a few logs to see intermediate results in the logger.
function keepUnique(){
var col = 3 ; // choose the column you want to use as data source (0 indexed, it works at array level)
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data=ss.getDataRange().getValues();// get all data
Logger.log(data);
var newdata = new Array();
for(nn in data){
var duplicate = false;
for(j in newdata){
if(data[nn][col] == newdata[j][0]){
duplicate = true;
}
}
if(!duplicate){
newdata.push([data[nn][col]]);
}
}
Logger.log(newdata);
newdata.sort(function(x,y){
var xp = Number(x[0]);// ensure you get numbers
var yp = Number(y[0]);
return xp == yp ? 0 : xp < yp ? -1 : 1;// sort on numeric ascending
});
Logger.log(newdata);
sh.getRange(1,5,newdata.length,newdata[0].length).setValues(newdata);// paste new values sorted in column of your choice (here column 5, indexed from 1, we are on a sheet))
}
EDIT :
Following Theodros answer, the spreadsheet formula is indeed an elegant solution, I never think about it but I should !!! ;-)
=SORT(UNIQUE(D1:D))
gives exactly the same result...
Currently, in V8 engine, the easiest way to do this is to use Set:
/**
* #returns {Object[]} Gets unique values in a 2D array
* #param {Object[][]} array2d
* #private
*/
const getUnique_ = array2d => [...new Set(array2d.flat())];
/**
* Gets Values from a column, makes it unique and sets the modified values
* to the next column
* #param {string} sheetName
* #param {number} column Number of the column to uniquify
* #param {number} headers Number of headers
* #returns void
*/
const uniquifyAColumn = (sheetName = 'Sheet1', column = 3, headers = 1) => {
const sh = SpreadsheetApp.getActive().getSheetByName(sheetName),
rg = sh.getRange(1 + headers, column, sh.getLastRow() - headers, 1),
values = rg.getValues(),
uniqueValues = getUnique_(values).map(e => [e]);
rg.offset(0, 1, uniqueValues.length).setValues(uniqueValues);
};
Here's a script for getting a unique list of nonempty values in a column given the Sheet it's on and the header location of the column using an array to store the values and indexOf to find duplicates. You can then write the array wherever you'd like.
headerLocation is a Location object:
var Location = function(sheet, row, col) {
this.sheet = sheet;
this.row = row;
this.col = col;
this.addRow = function() { this.row = this.row + 1; }
this.addCol = function() { this.col = this.col + 1; }
this.getValue = function() { return sheet.getRange(this.row, this.col).getValue(); }
this.toString = function() { return "(" + this.row + "," + this.col + ")"; }
}
This is the function to read the column and return unique values:
/**
* Get unique values in column, assuming data starts after header
* #param {Sheet} sheet - Sheet with column to search
* #param {object} headerLocation - row and column numbers of the column header cell
* #returns {array} list of unique values in column
*/
function getUniqueColumnValues(sheet, headerLocation) {
let startRow = headerLocation.row + 1;
let lastRow = sheet.getLastRow();
let values = [];
for (i = startRow ; i <= lastRow ; i++) {
let value = sheet.getRange(i, headerLocation.col).getValue();
if ((value != "") && (values.indexOf(value) == -1)) {
values.push(value);
}
}
return values;
}
Or, using a Location to find the values:
function getUniqueColumnValues(sheet, headerLocation) {
let values = [];
let searchLocation = new Location(sheet, headerLocation.row + 1, headerLocation.col);
let lastRow = sheet.getLastRow();
while (searchLocation.row <= lastRow) {
let value = searchLocation.getValue();
if (values.indexOf(value) == -1) {
values.push(value);
}
searchLocation.addRow();
}
return values;
}
I am trying to use Apps Script to query 2 datasets and compare certain columns across them. I am hoping to...
a) identify missing ID values;
b) reconcile differences in other fields, when the ID values match.
INPUT:
Spreadsheet with 2 tabs (tab1, tab2).
The key ID in each B column (Btab1, Btab2)
I want to identify instances where a unique value (B) is in one dataset but not in the other (the rows are not in the same order)
Run a function & push to an output tab if Btab1 is not in tab2 || Btab2 is not in tab1
When a value of B is in both tabs (the majority of the time), I want to identify instances of data discrepancies in a few columns...
For all instances of B, push B and the relevant columns below to the output tab if...
Column M in tab1 doesn't match column E in tab2
Column P in tab1 <> column F in tab2
Column AN tab1 <> Column G tab2
OUTPUT:
tab that displays problem areas in the datasets.
First column is ID Key.
Second column explains the issue via text string
Again, the challenge here is that the values are not sorted the same, and there could be a slight difference in total # rows
function compare() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);
ss.getActiveSheet().setName('output');
var sheet1 = ss.getSheetByName('sheet1');
var sheet2 = ss.getSheetByName('sheet2');
var sheet_output = ss.getSheetByName('output');
var range1 = sheet1.getRange(1,1,sheet1.getLastRow(),sheet1.getLastColumn()).getValues();
var output1 = [];
var a1;
var b1;
var h1;
var i1;
var j1;
var m1;
var o1;
var p1;
var an1;
var ao1;
var x;
var range2 = sheet2.getRange(1,1,sheet2.getLastRow(),sheet2.getLastColumn()).getValues();
var output2 = [];
var a2;
var b2;
var c2;
var d2;
var e2;
var f2;
var g2;
var h2;
var y;
/// can i do for(x in range1; y in range2) { all in one function?? If so, what is the proper syntax?
for(x in range1, y in range2) {
a1 = range1[x][0];
b1 = range1[x][1];
h1 = range1[x][7];
i1 = range1[x][8];
j1 = range1[x][9];
m1 = range1[x][12];
o1 = range1[x][14];
p1 = range1[x][15];
an1 = range1[x][39];
ao1 = range1[x][40];
a2 = range2[y][0];
b2 = range2[y][1];
c2 = range2[y][2];
d2 = range2[y][3];
e2 = range2[y][4];
f2 = range2[y][5];
g2 = range2[y][6];
h2 = range2[y][7];
if (
(b1 != b2) ||
(m1 != e2) // etc etc etc
)
{
//push to output
}}
Whilst your syntax for(x in range1, y in range2) will not return an error, it won't give you the desired result neither if the rows are not in the same order
Reason:
During each iteration both x and y will change, e.g. if var range1 = [1,2,3] and var range2 = [4,5,6], your loop will iterate 3 times and the values in your sample loop iterations will be:
iteration
range1[x] = 1 and range2[y] = 4
iteration
range1[x] = 2 and range2[y] = 5
iteration
range1[x] = 3 and range2[y] = 6
In this case you will not retrieve the combination
range1[x] = 1 and range2[y] = 4
or
range1[x] = 2 and range2[y] = 6
and so on.
Instead you need to use two nested for loops, which would iterate through all possible combinations of x and y:
for(x in range1) {
for(y in range2){
...
}
}
Sidenote:
Even if your rows would be in the same order, you still need to be careful. Because for(x in range1) opposed to for(x = 0; x < range1.length; i++) gives you no control about in which folder the loop will iterate over the range.
Now to your query for duplicates
A possible way to implement the functionality in a not too complicated manner would be the following:
Define boolean variable and use it to check for each x either it has a duplicate
If a duplicate (for column B) is found - further criteria will be evaluated
If two rows match by all criteria, the inner loop will be exited with break and the function will jump to the next x
If rows with identical key IDs, but discrepancies in other columns are found - both rows will be pushed into sheet output for comparison purposes (this is easier to implement than specifying what exactly is discrepant)
After this the inner loop will also be exited
In oth cases above duplicate will be set to true
If a unique Id is found in sheet1 (duplicate = false) - it will be immediately pushed into output
Sample
function compare() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);
ss.getActiveSheet().setName('output');
var sheet1 = ss.getSheetByName('sheet1');
var sheet2 = ss.getSheetByName('sheet2');
var sheet_output = ss.getSheetByName('output');
var range1 = sheet1.getRange(1,1,sheet1.getLastRow(),sheet1.getLastColumn()).getValues();
var output1 = [];
var b1;
var m1;
var p1;
var an1;
var x;
var range2 = sheet2.getRange(1,1,sheet2.getLastRow(),sheet2.getLastColumn()).getValues();
var output2 = [];
var b2;
var e2;
var f2;
var g2;
var y;
var array = [];
for(x in range1) {
var duplicate = false;
for(y in range2){
b1 = range1[x][1];
m1 = range1[x][12];
p1 = range1[x][15];
an1 = range1[x][39];
b2 = range2[y][1];
e2 = range2[y][4];
f2 = range2[y][5];
g2 = range2[y][6];
if (
(b1 == b2)
)
{
Logger.log("found");
duplicate = true;
if((m1 != e2)||
(p1 != f2) ||
(an1 != g2)){
array.push(range1[x]);
array.push(range2[y]);
}
break;
}
}
if (duplicate == false){
Logger.log("duplicate false");
array.push(range1[x]);
}
}
//push to output
if(array[0]){
sheet_output.getRange(sheet_output.getLastRow()+1, 1, array.length, array[0].length).setValues(array);
}
}
In Google Sheets, I have a spreadsheet called Events/Incidents which staff from various branches populate. I want Column B to automatically generate a unique ID based on the year in column A and the previously populated event. Given that there could be several events on a particular day, rows in column A could have duplicate dates.
The following is an example of what I am looking for in column B:
There can be no duplicates. Would really appreciate some help with either code or formula.
There are my thoughts https://github.com/contributorpw/google-apps-script-snippets/blob/master/snippets/spreadsheet_autoid/autoid.js
The main function gets a sheet and makes the magic
/**
*
* #param {GoogleAppsScript.Spreadsheet.Sheet} sheet
*/
function autoid_(sheet) {
var data = sheet.getDataRange().getValues();
if (data.length < 2) return;
var indexId = data[0].indexOf('ID');
var indexDate = data[0].indexOf('DATE');
if (indexId < 0 || indexDate < 0) return;
var id = data.reduce(
function(p, row) {
var year =
row[indexDate] && row[indexDate].getTime
? row[indexDate].getFullYear() % 100
: '-';
if (!Object.prototype.hasOwnProperty.call(p.indexByGroup, year)) {
p.indexByGroup[year] = [];
}
var match = ('' + row[indexId]).match(/(\d+)-(\d+)/);
var idVal = row[indexId];
if (match && match.length > 1) {
idVal = match[2];
p.indexByGroup[year].push(+idVal);
}
p.ids.push(idVal);
p.years.push(year);
return p;
},
{ indexByGroup: {}, ids: [], years: [] }
);
// Logger.log(JSON.stringify(id, null, ' '));
var newId = data
.map(function(row, i) {
if (row[indexId] !== '') return [row[indexId]];
if (isNumeric(id.years[i])) {
var lastId = Math.max.apply(
null,
id.indexByGroup[id.years[i]].filter(function(e) {
return isNumeric(e);
})
);
lastId = lastId === -Infinity ? 1 : lastId + 1;
id.indexByGroup[id.years[i]].push(lastId);
return [
Utilities.formatString(
'%s-%s',
id.years[i],
('000000000' + lastId).slice(-3)
)
];
}
return [''];
})
.slice(1);
sheet.getRange(2, indexId + 1, newId.length).setValues(newId);
}
I think it can be simplified in the feature.
There is an easier way to generate unique values that works for me, pick a #, then do +1. Ctrl C, then Ctrl shift V to paste back and remove the formula. Now you are left with thousands of unique IDs.
This is a manual solution but you can do an entire database in a matter of seconds every once in a while.
I'm looking for a tip for a AS3 script, have no idea how to start there
Button, if clicked the function is executed, which outputs a predefined value as the cross sum of a number string.
Example:
Cross sum should be 10
By clicking on the button, the function generates the number 55 or 82 or 37 or 523, ie numbers with the cross sum 10
An alternative way using % (modulo) instead of a string. You could write that into one line like this:
while (sum != 0) { qsum += sum % 10; sum /= 10; }
The trick is that modulo will give us only the last digit of the longer number, then we divide by 10 to trim off that last number (from longer) and we re-read a newer ending digit of the long number.
Example:
Long num = 1234, so each trim gives, 4 then 3 then 2 then 1 and we'll sum them up each time.
usage:
myInt = cross_sum(50); //makes myInt hold answer result of function (where ExpectedValue input is 50).
and the supporting function...
function cross_sum( ExpectedValue: int ) : int
{
var rand :int = Math.floor(Math.random() * 100000000000)
var sum :int = Math.abs( rand );
var qsum :int = 0;
while (sum != 0)
{
qsum += sum % 10; //get last digit of sum...
sum /= 10; //trim down sum by 1 digit...
}
if ( qsum == ExpectedValue ) { return rand; } //# stop here and give back "rand" as answer result.
else { cross_sum( expectedValue ); } //# else if wrong, try again...
}
Got it now.....
the function calculates a number, with the crosssum 50
function berechnen() {
var rand = Math.floor(Math.random() * 100000000000)
var sum = String(rand)
var qsum = 0;
for (var i = 0; i < sum.length; i++) {
qsum += Number(sum.charAt(i));
}
if (qsum == 50) {
summe.text = String(sum);
} else {
berechnen()
}
}
i have to handle unique names and save it in db like -
If name already exists, find the smallest number you can append to the name to save it as a unique name.
For example:
If username Scott already exists, save as Scott(1). If Scott(1) already exists, save as Scott(2). Etc.
i am using c# and sql server 2010
any great ideas ?
figured that out - but appreciate improvements
var seprator = "scott";
var list = new List<string>();
list.Add("scott");
list.Add("scott(1)");
list.Add("scott Alex");
list.Add("scott (4)");
list.Add("scott(xxx)");
list.Add("scott(250)");
list.Add("scott(12s)");
list.Add("scott(123)x");
list.Add("Scott caps");
list.Add("Alex Scott caps");
list.Add("xxxscottmmm");
var numberList = new List<int>();
foreach (var v in list)
{
var parts = Regex.Split(v, seprator, RegexOptions.IgnoreCase);
if (parts.Length > 1) //(1) Alex (4) (xxx) (250) (12s) (123)x caps caps
{
var secondPart = parts[1].Trim();
if (secondPart.StartsWith("(") && secondPart.EndsWith(")")) // (1) (4) (xxx) (250) (12s)
{
var valuebetweenbraces = secondPart.Substring(1, secondPart.Length - 2); //1 4 xxx 250 12s
int number;
var isNumber = int.TryParse(valuebetweenbraces, out number);
if (isNumber)
{
numberList.Add(number);
}
}
}
}
int maxValue = 0;
if (numberList.Count > 0)
maxValue = numberList.Max() + 1;
else
maxValue = maxValue + 1;
Response.Write(seprator + "(" + maxValue + ")");