Trying to sort and push only the distinct values into a new array and then find the length of the resulting array. See code below. The UniqueTeams.length is coming out to be 1, when it should be 5 (I thought).
var teams = [[formSS.getRange("F8").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("F12").getValue(),
formSS.getRange("F14").getValue(),
formSS.getRange("F16").getValue()]];
var uniqueTeams = removeDups(teams);
if(uniqueTeams.length < 5){
{SpreadsheetApp.getUi().alert(uniqueTeams);
return;}
}
function removeDups(array) {
var outArray = [];
array.sort();
outArray.push(array[0]);
for(var n in array){
if(outArray[outArray.length-1]!=array[n]){
outArray.push(array[n]);
}
}
return outArray;
}
uniqueTeams = Alabama (2),Maryland (10),Cleveland St (15),BYU (6),Liberty (13)
uniqueTeams.length = 1
Get unique elements
function remdup() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const t = sh.getRange('F8:F16').getValues().flat();
let teams = [t[0], t[2], t[4], t[6], t[8]];
let s = new Set(teams)
teams = [...s];
Logger.log(teams.join(','))
}
Set
Cooper's answer it the best ultimate solution. But if you want just to fix your code you can try to change the line:
var uniqueTeams = removeDups(teams);
with:
var uniqueTeams = removeDups(teams[0]);
Since the teams is a 2D array [[...]].
Related
I am creating two lists of links on my opening sheet (9060DASH) in Google Sheets. It looks like this:
One list is links to all sheets that are not hidden (Active) and one to all that are hidden. Each item must meet two other criteria to be included:
The sheet name must not include the numbers "9060."
The cell GH3 in the sheet must contain "Protected."
I have a functioning script, but it is loading too slowly. Here is a sample to show how it loads during onOpen(). How can I do this more efficiently? Can it be done better using "push"? Here is the script:
function populateSheetList() {
SpreadsheetApp.getActive().getSheetByName("9060DASH").activate();
var ss = SpreadsheetApp.getActive();
var ui = SpreadsheetApp.getUi();
ss.getRange('9060DASH!D4:E100').clear();
var counter = 4;
var sheetName = "";
var cellID= "";
var richValue = "";
var sheetURL = ss.getUrl();
var sheetLink = ""
var mySheet = "";
var shouldNotContain = '9060'; //array code from stackover to build array without 9060 sheets
var sheetNames = SpreadsheetApp.getActiveSpreadsheet().getSheets().map(s => s.getName());
var filtered = sheetNames.filter(x => !x.toLowerCase().match(shouldNotContain.toLowerCase()));
/** CREATES ACTIVE SHEETS LIST */
for(var i =0;i<filtered.length;i++){
cellID = '9060DASH!D'+counter;
sheetName = filtered[i];
mySheet = ss.getSheetByName(sheetName);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
if(mySheet.isSheetHidden() == false){
if(ss.getRange(sheetName+"!GH3").getValue() == "Protected"){
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
ss.getRange(cellID).setRichTextValue(richValue);
counter = counter+1;
}
}
}
/** GATHERING HIDDEN SHEETS */
var counter = 4;
for(var i =0;i<filtered.length;i++){
cellID = '9060DASH!E'+counter;
sheetName = filtered[i];
mySheet = ss.getSheetByName(sheetName);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
if(mySheet.isSheetHidden() == true){
if(ss.getRange(sheetName+"!GH3").getValue() == "Protected"){
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
ss.getRange(cellID).setRichTextValue(richValue);
counter = counter+1;
}
}
}
}
I have reduced the time to load the dashboard by rearranging a number of things and by getting a final array before writing anything out to the dash. I reformatted the cells and sorted the records before the evaluation loops began. I added more status messages, to keep users engaged while the background work was going on. I put the criteria of separating active from hidden sheets last and, depending on which, wrote the values to two different arrays (actvSheets and hidnSheets). Then I processed each array out to the dashboard. I did this in for loops. I suspect I could save even more time to write them out as one command from each array, but I have not been able to figure out how to write out in vertical form. Here is the code. Any refinements are welcome!
/**
* =================
* populateSheetList
* =================
* This function builds the available sheets in the
* 9060DASH sheet--containing all with the Protected
* status.
*/
function populateSheetListNEW() {
msgDash("Clearing sheet list . . . ")
// SpreadsheetApp.getActive().getSheetByName("9060DASH").activate();
var ss = SpreadsheetApp.getActive();
var ui = SpreadsheetApp.getUi();
ss.getRange('9060DASH!D4:E100')
.clear()
.setFontSize(14)
.setHorizontalAlignment('left')
.sort({column: 4, ascending: true})
msgDash("Evaluating sheets . . . ")
var counter = 4;
var sheetName = "";
var cellID= "";
var richValue = "";
var sheetURL = ss.getUrl();
var sheetLink = ""
var mySheet = "";
var actvSheets = [];
var hidnSheets = [];
msgDash("Eliminating administrative sheets . . . ")
var shouldNotContain = '9060'; //array code from stackover to build array without 9060 sheets
var sheetNames = SpreadsheetApp.getActiveSpreadsheet().getSheets().map(s => s.getName());
var filtered = sheetNames.filter(x => !x.toLowerCase().match(shouldNotContain.toLowerCase()));
var sortFilt = filtered.sort();
/** CREATES SHEET ARRAYS */
msgDash("Gathering census worksheets . . . ")
for(var i =0;i<sortFilt.length;i++){
sheetName = sortFilt[i];
mySheet = ss.getSheetByName(sheetName);
if(ss.getRange(sheetName+"!GH3").getValue() == "Protected"){
if(mySheet.isSheetHidden() == false){
actvSheets.push(sheetName);
} else {
hidnSheets.push(sheetName);
}
}
}
msgDash("Gathering active sheets . . . ")
/** WRITING DATA FROM ACTVSHEETS ARRAY */
for(var j = 0; j < actvSheets.length; j++){
sheetName = actvSheets[j];
mySheet = ss.getSheetByName(actvSheets[j]);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
cellID = '9060DASH!D' + counter;
counter = counter+1;
ss.getRange(cellID).setRichTextValue(richValue);
}
msgDash("Gathering hidden sheets . . . ");
/** WRITING DATA FROM HDDNSHEETS ARRAY */
counter = 4;
for(var k = 0; k< hidnSheets.length; k++){
sheetName = hidnSheets[k];
mySheet = ss.getSheetByName(hidnSheets[k]);
sheetLink = sheetURL+'#gid='+ mySheet.getSheetId();
richValue = SpreadsheetApp.newRichTextValue()
.setText(sheetName)
.setLinkUrl(sheetLink)
.build();
cellID = '9060DASH!E' + counter;
counter = counter+1;
ss.getRange(cellID).setRichTextValue(richValue);
}
msgDash('Dashboard ready. Enjoy your census work!');
ss.getRange('9060DASH!D2').setValue("For hidden sheets, respond to 'Unhide' instruction. Click Refresh button to rebuild list.");
Utilities.sleep(20);
msgDash(''); //clears messages
}
Let say I have 3 arrays,
compCD = ["909", "908", "080", "901"];
contNO = ["09999", "08888", "00777", "00666"];
pomNum = ["A01", "A02", "A03", "A04"];
How can I insert into jsonMap each separated Values like
[
{
"compCD" = "909",
"contNO" = "09999",
"pomNum" = "A01"
},{
"compCD" = "908",
"contNO" = "08888",
"pomNum" = "A02"
}
]
we can assume that each array size is the same. The first of each array will bi insert first to map.
How we can solve those with minimum loop.
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=[];
for(var i=0; i<compCD.length; i++) {
jsonMap.push(
{compCD:compCD[i], contNO:contNO[i], pomNum:pomNum[i]}
);
};
console.log(jsonMap);
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=compCD.map(
(currentValue, index)=>[currentValue, contNO[index], pomNum[index]]
);
console.log(jsonMap);
I'm trying create new random List based on other List. But this code doesn't work to update the var
import scala.util.Random
import scala.math
val randSymbol = List(1,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8)
val _finalSymbol = List(List(0,0,0,0,0),List(0,0,0,0,0),List(0,0,0,0,0))
var i:Int = 0
var a:Int = 0
for (i <- 0 to 2){
_finalSymbol(i) = new List
for (a <- 0 to 4){
var iRandIndex = floor(Random.nextInt() * randSymbol.length).toInt
var iRandSymbol = randSymbol(iRandIndex)
_finalSymbol(i)(a) = iRandSymbol
}
}
Try something like this:
val _finalSymbol = (0 to 2) map { _ => Random.shuffle(randSymbol).take(5) }
And do yourself a favor: buy a scala book.
It's not javascript. At all.
Is there a way to make the code below work properly? When I use this code it only shows one movieclip:
var tempHead:head001 = new head001();
var mcArr:Array = new Array( tempHead );
var firstHead:MovieClip = mcArr[0];
firstHead.y = 30;
addChild(firstHead);
var secondHead:MovieClip = mcArr[0];
secondHead.y = 180;
addChild(secondHead);
`
You were just assigning a reference to the MovieClip. That'y, its not working.
First take instance of head001 class using new operator as much you want and store it to an array, then you can access very easily.
var tempHead: head001;
var mcArr: Array = new Array();
for (var i: uint = 0; i < 2; i++) {
tempHead = new head001();
addChild(tempHead);
mcArr.push(tempHead);
mcArr[i].y = mcArr[i].height * i;
}
Example of the my problem.
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
// and the out come I want it to be is this
trace(array_1[0]) // 2,4,6,8,10,12,14,16,20,22,24
// I did try array_1[0] += array_2[0] but it didn't work currently
Any suggestion would be great.
This will perform what you are looking for and also allows you to add multiple rows of data to array_1 or array_2
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:Array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
var combinedArray:Array = new Array();
for( var i:int = 0; i < array_1.length; i++ ) {
combinedArray = combinedArray.concat(array_1[i]);
}
for( i = 0; i < array_2.length; i++ ) {
combinedArray = combinedArray.concat(array_2[i]);
}
trace(combinedArray);
As stated in the comments, you can use the concat method:
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
array_1[0] = array_1[0].concat(array_2[0]).concat(array_2[1]);
This, of course, is very messy looking. I am wondering why you are storing arrays inside of other arrays for no discernible reason.