Log repeated values in 2d array - javascript

I'm trying to optimise a whole school timetable. I have the timetable currently organised in a sheet. Teacher initials are the headings for each column and each row corresponds to a single teaching period in a 30 lesson week. Each cell contains the name of a class.
Currently I am looking for classes that are split between 2 teachers.
I am trying to make an appscript that will log the classname if it appears anywhere outside the current column (i.e. the same class is being taught by 2 or more different teachers at different times)
I'm aware that nesting loops is not the least efficient way of doing this but I just wanted to hack something together quickly to get the job done. Unfortunately this code is taking longer than the maximum permitted time. The array is only 30 rows by about 56 columns so I dont see why it's taking such a long time. (Cant see anything that's obviously infinite about my loops either)
Can anyone help? :)
function splitClassLocator()
{
//copy the sheet to a 2d array.
//(1)descend through each column from vertical idx 3 to period6 idx36
//(2)start at horiz idx 1, descend through each item vertically.
//if item from loop 1 matches item from loop 2 and loop 1 vertical index != loop 2 vertical index
//log the item (split class)
//GET THE DATA
var sh0 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var range = sh0.getDataRange();
var data = range.getValues();
//COMPARE
//main row mr, main col mc V compare row cr, compare col cc
Logger.log("Rows = " + data.length + "cols = " + data[0].length);
for (var mr = 2; mr < data.length; mr ++)
{
for (var mc = 1; mc < data[0].length; mc ++)
{
for (var cr = 2; cr < data.length; cr ++)
{
for (var cc = 1; cc, data[0].length; cc ++)
{
if (mc != cc) // if it's not comparing classes belonging to the same teacher
{
if ((data[mr][mc] != undefined) || (data[mr][mc] != null) || (data[mr][mc] != ""))
{
if (data[mr][mc] == data[cr][cc])
{
Logger.log(data[mr][mc]);
}
}
}
}
}
}
}
}

Rather than discard the information after your comparison, store it and use it! Welcome to the world of not-Arrays!
Your stated goal is to find classes that have more than two teachers. Thus, the minimum you need to do is tally this information on a single trip through the array.
function countTeachersPerClass() {
const schedule = SpreadsheetApp.getActive().getSheetByName("somename").getDataRange().getValues();
const headers = schedule.shift();
const numTeachers = headers.length;
// Store classes as properties in an Object.
const classes = {};
// Read the schedule array.
// Assumption: time is in index 0, classes in all other.
schedule.forEach(function (period) {
var name = period[0];
// Store each class in this period.
for (var teacherIndex = 1; teacherIndex < numTeachers; ++teacherIndex) {
// Have we seen this class? If no, initialize it.
var classID = period[teacherIndex];
if (!classes[classID])
classes[classID] = {teachers: {} };
// Add this teacher to its list of teachers.
var tID = headers[teacherIndex];
if (!classes[classID].teachers[tID])
classes[classID].teachers[tID] = {periods: []};
// Add this period for this teacher.
classes[classID].teachers[tID].periods.push(name);
} //End for loop over columns in a row.
}); // End forEach over schedule's rows.
// Log the object (in Stackdriver, for easy review and interactivity).
console.log({message: "Built classes object", input: schedule, result: classes, teachers: headers});
// Make a report from it.
const report = [["Class ID", /* other headers */]];
for (var cID in classes) {
// This particular report is for class with more than two teachers.
if (Object.keys(classes[cID].teachers).length > 2) {
var row = [cID];
/** Push info to the row, perhaps with just the names
of the teachers, or also including number of
periods per each teacher, etc. */;
// Add the completed report row to the report.
report.push(row);
}
}
// Log the report.
console.info({message: "Report", report: report});
}
You could certainly get fancier by adding more properties to each classes object than just teachers, such as tracking the average consecutive taught time (i.e. which classes alternate teachers often), but I leave that as an exercise to the reader :)

This works a bit better, flattening to make 2 single dimensional arrays, one is the teacher to check and the other array is all the other timetables combined.
Still takes ages though:
function splitClasses()
{
var sh0 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
var range = sh0.getDataRange();
var data = range.getValues();
var rows = data.length;
var cols = data[0].length;
for (var col = 1; col < cols; col ++)
{
var teacherTT = [];
var othersTT = [];
for (var row = 2; row < rows; row ++)
{
teacherTT.push(data[row][col]);
}
for (var oCol = 1; oCol < cols; oCol ++)
{
for (var oRow = 2; oRow < rows; oRow ++)
{
if (col !=oCol)//dont add current teacher TT to others TT
{
othersTT.push(data[oRow][oCol]);
}
}
}
//Logger.log(othersTT);
var tLength = teacherTT.length;
var oLength = othersTT.length;
Logger.log("tL" + tLength);
Logger.log("oL" + oLength);
for (var t = 0; t < tLength; t ++)
{
//Logger.log("t "+t);
for (var o = 0; o < oLength; o ++)
{
if (teacherTT[t] != undefined ||teacherTT[t] != null || teacherTT[t] != "" || teacherTT[t] != " ")
{
if (teacherTT[t])
{
if (teacherTT[t] == othersTT[o])
{
//Logger.log("o "+o);
Logger.log(teacherTT[t]);
}
}
}
}
}
}
}

Related

object's property different then when assigned even though no other changes are made (checked with implemetation of object.watch)

Object property value differentiates from the value I'm assigning to it,even though none other changes are made to the property which I checked with implementation of Object. watch I found at this link Watch for object properties changes in JavaScript.
From sofascore I'm getting an array with weeks (objects with start and end time) and I want to add home and away standings to each week as they were at the end of that week.I'm getting matches and their start time and result also from sofascore.
My sorting algorithm which sorts the table and pushes teams to their index in standings.home or standings.away array is working as expected.
I've worked on this for a couple hours now and a couple of hours last night and I think that I've already tried every possible solution that I can think of.
const https = require('https');
function get(url) {
return new Promise( resolve => {
https.get(url , res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {body += data;});
res.on("end", () => {resolve(body)});
});
});
}
function standingsByWeek(tournamentID,seasonID) {
const baseURL = 'https://www.sofascore.com/';
var league,weeks,matches,teams,teamsIDs,standings,currentWeekIndex = 0,table = {},possiblePoints = [3,1,0];
get( baseURL + 'u-tournament/' + tournamentID + '/season/' + seasonID + '/json').then(function(league) { //get data for league
league = JSON.parse(league);
weeks = league.events.weeks;
teams = league.teams;
for (let i = 0; i < teams.length; i++) { //fill up table with teams
table[teams[i].id] = { id : teams[i].id, home : [0,0,0,0,0,0,teams[i].id], away : [0,0,0,0,0,0,teams[i].id]} ; // wins,draws,losses,score,conceded,points
};
teamsIDs = Object.keys(table); //extract keys (teamsIDs) from table
return get( baseURL + 'u-tournament/'+tournamentID+'/season/'+seasonID+'/matches/week/'+weeks[0].weekStartDate+'/'+weeks[weeks.length-1].weekEndDate)// get data for matches
}).then(function(matches) {
matches = JSON.parse(matches).weekMatches.tournaments[0].events; // assign matches to matches
for (let i = 0; i < matches.length; i++) { //update the table for every finished match
with (matches[i]){
if(status.code != 100){continue} // continue if match not finished
var outcome = Math.sign(homeScore.normaltime - awayScore.normaltime); // get outcome 1 = win , 0 = draw , -1 lose (for home team)
table[homeTeam.id].home[Math.abs(outcome - 1)] ++; // incrementing number at index for outcome
table[awayTeam.id].away[outcome + 1] ++; // -||-
table[homeTeam.id].home[3] += homeScore.normaltime; // adding scored goals in home team's home table
table[homeTeam.id].home[4] += awayScore.normaltime; // adding conceded goals in home team's home table
table[awayTeam.id].away[3] += awayScore.normaltime; // adding scored goals in away team's away table
table[awayTeam.id].away[4] += homeScore.normaltime; // adding conceded goals in away team's away table
table[homeTeam.id].home[5] += possiblePoints[Math.abs(outcome - 1)]; // adding earned points to home team's home table
table[awayTeam.id].away[5] += possiblePoints[Math.abs(outcome + 1)]; // adding earned points to away team's away table
}
// if this is the last match of the season or current week ended ,sort the table and push standings to that week and increase current week index
if ( (i + 1) == matches.length || weeks[currentWeekIndex].weekEndDate < matches[i + 1].startTimestamp ) {
standings = {home : [], away:[] };
for (let j = 0; j < 2; j++) { // loop through both keys in standings
var HoA = Object.keys(standings)[j]; //home or away
k : for (let k = 0; k < teamsIDs.length; k++) { // loop through all teams in the table
for (let l = 0; l <= standings[HoA].length; l++) { // loop through all teams added to standings
var EoL = Boolean(l == standings[HoA].length) // empty or last
var tableTeam = table[teamsIDs[k]][HoA] ;
var standingsTeam = EoL ? null : standings[HoA][l]; //standings team or null if standings is empty
var pointDiff = l == standings[HoA].length ? null : Math.sign(tableTeam[5] - standingsTeam[5]) ; // point difference 1 = table team has more, 0 = equal # of points, -1= standings team has more points
// standings empty(or last) or more points or equal points and better goal difference
if ( EoL || (pointDiff == 1) || !pointDiff && (tableTeam[3] - tableTeam[4]) > (standingsTeam[3] - standingsTeam[4] ) ) {
standings[HoA].splice(l,0,table[teamsIDs[k]][HoA]); // put team from table to index at which team which it was compared to was
continue k // go to another team
}
}
}
}
// USING THIS INSTEAD OF LINE BELOW IT YOU CAN SEE WHAT VALUE IS BEING ASSIGNED TO STANDINGS PROPERTY OF WEEK OBJECT
//Object.defineProperty(weeks[currentWeekIndex],'setter',{set: function (value) {this.standings = value;console.log(currentWeekIndex,value);}})
// weeks[currentWeekIndex].setter = Object.assing({},standings);
weeks[currentWeekIndex].standings = Object.assign({},standings);// assigning standings clone to standing property of current week
currentWeekIndex++;// increase week index
}
}
console.log(weeks[0].standings);
}
)
}
standingsByWeek(17,13380); // premier league , season 17/18
I expect object property to have the same value as the assigned value if none other changes to property are made.
EDIT
link to tidied and runnable version of the code https://repl.it/#mwittig/objectPropertiesChange
(thanks to Marcus)

How to append data to end of rows in Google Apps Script

One of our high schools is trying to create weekly reports for students who are currently failing one or more classes. The reports are in the format linked below.
If a student is failing multiple classes, the information is on different rows. The information needs to be combined, with each student on one column - column 1 is the name, and the subsequent columns are for the grade/class/teacher information.
Currently, I have code that deletes blank sheets, creates a new sheet titled "Output", writes a header row, and writes unique names in column 1 (portions commented out so it doesn't create a duplicate "Output" sheet every time):
function copyRows() {
//Initialize variables
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName("Sheet1");
/*
//Create output Sheet
ss.insertSheet("Output");
*/
var writeSheet = ss.getSheetByName("Output");
/*
//Delete unwanted columns (1, 3, 4, 7, 9)
dataSheet.deleteColumn(1); //Deletes first column
dataSheet.deleteColumn(2); // Deletes third column (because it shifts after every deletion)
dataSheet.deleteColumn(2); //Deletes fourth column
dataSheet.deleteColumn(4); //Deletes 7th column
dataSheet.deleteColumn(5);//Deletes 9th column
//Delete unwanted Sheets
var deleteSheet2 = ss.getSheetByName("Sheet2");
var deleteSheet3 = ss.getSheetByName("Sheet3");
ss.deleteSheet(deleteSheet2);
ss.deleteSheet(deleteSheet3);
//Write data to header row
var headerRange = writeSheet.getRange("A1:V1");
var headerValues = [
["name", "class1", "grade1", "teacher1","class2", "grade2", "teacher2","class3", "grade3", "teacher3","class4", "grade4", "teacher4","class5", "grade5", "teacher5","class6", "grade6", "teacher6","class7", "grade7", "teacher7"]
]
headerRange.setValues(headerValues);
*/
var lastRow = dataSheet.getLastRow();
var mainArray = dataSheet.getRange(1, 1, lastRow, 4).getValues();
var allNames = []; //List of all names, including duplicates
var uniqueNames = []; //List of all unique names
for (i = 0; i < mainArray.length; i++) { //Sets allNames
allNames.push(mainArray[i][0]);
}
for (i = 0; i < allNames.length; i++) { //Sets uniqueNames
if (allNames[i+1] != allNames[i]) {
uniqueNames.push(allNames[i]);
}
}
var uniqueNamesArray = uniqueNames;
//New method that converts 1d array to 2d array
Array.prototype.reshape = function(rows, cols) {
var copy = this.slice(0); // Copy all elements.
this.length = 0; // Clear out existing array.
for (var r = 0; r < rows; r++) {
var row = [];
for (var c = 0; c < cols; c++) {
var i = r * cols + c;
if (i < copy.length) {
row.push(copy[i]);
}
}
this.push(row);
}
};
var uniqueNamesRow = uniqueNames;
uniqueNames.reshape(uniqueNames.length, 1); //Changing uniqueNames from row to column
var writeNamesRange = writeSheet.getRange(2,1,uniqueNames.length,1); //writeSheet column 1
writeNamesRange.setValues(uniqueNames);
Example data:
John Doe 50 Band Mr. Dean
Mary Smith 60 US History Ms. Jones
Mary Smith 25 Chemistry Ms. Dyar
Mary Smith 40 Algebra 2 Ms. Harris
Bob Miller 55 Band Mr. Dean
Larry Jones 22 Algebra 2 Ms. Harris
With the output of:
John Doe 50 Band Mr. Dean
Mary Smith 60 US History Ms. Jones 25 Chemistry Ms. Dyar 40 Algebra 2 Ms. Harris
Bob Miller 55 Band Mr. Dean
Larry Jones 22 Algebra 2 Ms. Harris
Note that Mary Smith's data has been combined into one row.
I just cannot figure out how to iterate through the rows and append the data to the end of the appropriate row.
Sorry for the previous lack of details, and thanks for the feedback.
I wrote some codes based on your input and output (not on your code). Since student's name is a primary key, I think dictionary data structure is suitable here. Below code is not so elegant and many literals were hard-coded but I guess you'll be able to grasp the idea. Sample sheet is here.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('input');
var rows = 6;
var cols = 4;
var input = ss.getRange(1,1, rows,cols).getValues();
var students = [];
for(var i = 0; i < input.length; i++) {
var name = input[i][0];
var class = {
score: input[i][1],
class: input[i][2],
teacher: input[i][3]
};
var j;
for(j = 0; j < students.length; j++) {
if(students[j].name == name) {
students[j].classes.push(class);
break;
}
}
if(j == students.length) {
students.push({
name: name,
classes: [class]
});
}
}
var ts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('output');
var output = [];
for(var i = 0; i < students.length; i++) {
output[i] = [students[i].name];
for(var j = 0; j < students[i].classes.length; j++) {
output[i].push(students[i].classes[j].score,
students[i].classes[j].class,
students[i].classes[j].teacher);
}
ts.getRange(i+1,1, 1,output[i].length).setValues([output[i]]);
}
}
I tried this for a while, and came up with a different approach. Below in case someone finds it useful, I also added some comments
function appendToRecords2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetWithNewData = ss.getSheetByName("New");
var sheetWithExistingData = ss.getSheetByName("Existing")
// Defines the range where the NEW DATA is located
var newData = sheetWithNewData.getRange("a2:b25")
.getValues();
var lastRow = sheetWithExistingData.getLastRow();
sheetWithExistingData.getRange(lastRow + 1, 1, 24, 2)
.setValues(newData);
/*
Parameters for the last to one row
1st = +1, this is always the same. This indicates that it's last row + 1
2nd = The column where to copy the data, normally 1
3rd = The number of rows that contains the new data. This is pretty much the same as the rage defined in getRange
4th = The number of columns that contains the data, same as above
*/
}
IMPORTANT: All credit goes to #jvdh from his answer at Script to append range of data in Google Sheets. I only made some minor changes and added the comments clarifications

Why is my code executing far more times than it's supposed to?

I'm currently working on a poker odds generator and it's pretty much done, except for one thing. The program runs far more often than it should. I know that this is coming from the compare() function, because when I add a variable Q to keep track of the number of times the main function playPoker() has run, it produces a huge number until I comment it out - at which point it returns the exact number I'd expect.
Can any of you point out where I'm going wrong with this. I can't see why one function should lead to Q being incremented so much more than it should be. Literally, the last time I ran it the number was (32,487 instead of 100). I present the code below (without the poker hand-checking functions because they're not really important). Why is playPoker() running so many times?!
var Q = 0;
function playPoker(tableSize) {
//Create the players, the deck and the card table which stores the 5 cards the players have in common
var players = createPlayers(tableSize);
var deck = createDeck();
var cardTable = new CardTable();
//Deal each player two cards
for (i = 0; i < 2; i++) {
for (j = 0; j < players.length; j++) {
deal(deck, players[j]);
}
}
//Put five cards down on the table
for (k = 0; k < 5; k++) {
deal(deck, cardTable);
}
//Check for various winning hands here for each player
for (m = 0; m < players.length; m++) {
//Merge the player's two cards with the five cards on the table
var subjectCards = (players[m].cards).concat(cardTable.cards);
//Make an array of the values of each of the seven cards, which will be used to determine 4 of a kind, 3 of a kind and pairs
var valuesInOrder = getValuesInOrder(subjectCards);
//Create a dummy array, so that valuesInOrder remains unchanged
var straightValues = valuesInOrder.slice();
//Remove any duplicate card, meaning that the array contains only unique values (i.e. 2, 4, 5, 7, K ... NOT 2, 2, 2, 8, K, K, A)
var straightValues = straightenUp(straightValues);
//Calculate how many pairs are in the hand
var numPairs = howManyPairs(valuesInOrder, straightValues, players[m]);
//Find out whether the 5 table cards contain three cards of the same suit. If not, then a flush is impossible.
var flushPotential = threeSameSuit(cardTable.cards);
//Find out which hand each player has (i.e. straight, 3OAK, pair)
checkPokerHand(subjectCards, straightValues, valuesInOrder, flushPotential, numPairs, players[m])
}
var rankedPlayers = compare(players);
//return players;
Q++;
return Q;
}
And here's the for-loop that sets it off.
for (z = 0; z < 100; z++;) {
playPoker(4);
}
And here's the compare() function:
function compare(players) {
var remPlayers = players.slice();
var rankings = [];
var potentialWinners = [];
//Collect all the players' rankings in an array
for (i = 0; i < remPlayers.length; i++) {
rankings.push(remPlayers[i].rank);
}
//Find the highest ranking
var highestRank = getHighestValue(rankings);
//Move any players with the highest ranking to an array for potential winners
for (j = 0; j < remPlayers.length; j++) {
if (remPlayers[j].rank == highestRank) {
potentialWinners.push(remPlayers[j]);
remPlayers.splice(j, 1);
j--;
}
}
//With all potential winners gone, mark all other players with an L for losing.
for (k = 0; k < remPlayers.length; k++) {
remPlayers[k].result = 'L'
}
var foundWinner = false;
if (potentialWinners.length < 2) {
potentialWinners[0].result = 'W';
foundWinner = true;
}
//Check there is more than one potential winner. If not, the only guy in the array has won.
if (!foundWinner) {
//Loop through all players first cards and find the highest value, then delete any who don't have that, marking them with 'L'.
//If there is no single remnant, do the same for second cards, then third, then fourth, etc.
for (p = 0; p < 5; p++) {
var subRankings = [];
for (q = 0; q < potentialWinners.length; q++) {
subRankings.push(potentialWinners[q].bestHand[p]);
}
var highestSubRanking = getHighestValue(subRankings);
//Mark 'L' and remove any player who does not meet the highest subranking
for (m = 0; m < potentialWinners.length; m++) {
if (potentialWinners[m].bestHand[p] < highestSubRanking) {
potentialWinners[m].result = 'L';
potentialWinners.splice(m, 1);
}
if (potentialWinners.length < 2) {
potentialWinners[0].result = 'W';
//Set this flag to true to break the loop because a winner has been found
foundWinner = true;
break;
}
}
//Break the loop if we have found a winner
if (foundWinner) {
break;
}
//If we still haven't found a winner by the end of the 5th loop, all remaining players draw
if (p == 4) {
for (z = 0; z < potentialWinners.length; z++) {
potentialWinners[z].result = 'D';
}
}
if (foundWinner) {
break;
}
}
}
return players;
}
Try using var declarations on your variables to manage their scope within their relevant functions?

Javascript - Associative Arrays not working?

So I have csv files that basically include lists of students, what school they are in, and what subjects they are taking(ex. chem, spanish, biology, etc). I want to have my program let the user type in a subject area(s) and have the page return the amount of times each subject is being taken.
I then have some javascript that basically takes in some user input from a text field, parse that, and puts it into an array. Then, it imports the csv file and compares the subject columns to what the user inputs, and calculates the amount of time each subject is taking.
My javascript looks like this:
var globalArray = [];
var schoolList = [];
var splitTextInput = [];
var count = 0;
var splitSubjectArea = [];
function myFunction()
{
var textInput = document.getElementById('numb').value;
var needsTrimTextInput = textInput.split(","); //creating an array to store user input
for( var q = 0; q < needsTrimTextInput.length; q++) //getting rid of whitespace in user input
{
splitTextInput[q] = needsTrimTextInput[q].trim();
}
for(var j = 0; j< splitTextInput.length; j++)
{
var sSubjectArea = {};
sSubjectArea[ splitTextInput[j] ] = 0; //assigning the value to 0 to store the count of each subject
}
var fileName = document.getElementById("UniversitySelect").value;
if( fileName.indexOf(".csv") > 0 )
{
d3.csv( "./" + document.getElementById("UniversitySelect").value, bob, counting);
}
function bob(d){
return { Area: d.Area };
}
function counting(error, rows)
{
globalArray = rows;
for( var i = 0; i < rows.length; i++ ) //for the row in the CSV file
{
for( var k = 0; k < splitTextInput.length; k++ ) // loop to go through the different inputed subject areas
{
if( rows[i].Area.toLowerCase().indexOf( splitTextInput[k].toLowerCase() ) > -1)
{
count++; //stores the overall count
sSubjectArea[splitTextInput[k]] += 1;
//console.log(sResearchArea[splitTextInput[k]]);
}
}
}
console.log(rows);
for(r = 0; r < splitTextInput.length; r++)
{
console.log( sSubjectArea[ splitTextInput[r] ] );
}//for
}//function
}//function
I know it partially works, because if I enter in 2 subjects, say Chemistry and Biology, which are taken 3 times each, then the count will be 6. However, I cannot get sSubjectArea to hold the count for each individual subject. What am I doing wrong? When I added the console.log(sSubjectArea[splitTextInput[k]]) line, the output I get is:
1
NaN
2
3
4
NaN
5
6
Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 86 moreā€¦ ]
NaN
6
And I don't really understand why I get those numbers & especially NaN.... can anyone help? I'm really new to javascript, so I might have made some sort of fundamental error in understanding objects, but I can't figure it out. Any help would be much appreciated, thanks!!

How can I remove rows with unique values, keeping rows with duplicate values?

I have a spreadsheet of surveys, in which I need to see how particular users have varied over time. As such, I need to disregard all rows with unique values in a particular column. The data looks like this:
Response Date Response_ID Account_ID Q.1
10/20/2011 12:03:43 PM 23655956 1168161 8
10/20/2011 03:52:57 PM 23660161 1168152 0
10/21/2011 10:55:54 AM 23672903 1166121 7
10/23/2011 04:28:16 PM 23694471 1144756 9
10/25/2011 06:30:52 AM 23732674 1167449 7
10/25/2011 07:52:28 AM 23734597 1087618 5
I've found a way to do so in Excel VBA:
Sub Del_Unique()
Application.ScreenUpdating = False
Columns("B:B").Insert Shift:=xlToRight
Columns("A:A").Copy Destination:=Columns("B:B")
i = Application.CountIf(Range("A:A"), "<>") + 50
If i > 65536 Then i = 65536
Do
If Application.CountIf(Range("B:B"), Range("A" & i)) = 1 Then
Rows(i).Delete
End If
i = i - 1
Loop Until i = 0
Columns("B:B").Delete
Application.ScreenUpdating = True
End Sub
I'd like to do it in Google Spreadsheets with a script that won't have to be changed. Closest I can get is retrieving all duplicate user ids from the range, but can't associate that with the row. That code follows:
function findDuplicatesInSelection() {
var activeRange = SpreadsheetApp.getActiveRange();
var values = activeRange.getValues();
// values that appear at least once
var once = {};
// values that appear at least twice
var twice = {};
// values that appear at least twice, stored in a pretty fashion!
var final = [];
for (var i = 0; i < values.length; i++) {
var inner = values[i];
for (var j = 0; j < inner.length; j++) {
var cell = inner[j];
if (cell == "") continue;
if (once.hasOwnProperty(cell)) {
if (!twice.hasOwnProperty(cell)) {
final.push(cell);
}
twice[cell] = 1;
} else {
once[cell] = 1;
}
}
}
if (final.length == 0) {
Browser.msgBox("No duplicates found");
} else {
Browser.msgBox("Duplicates are: " + final);
}
}
This is maybe not very efficient, but I think it's what you want:
var ar=[1,3,3,5,6,8,6,6];
console.log("Before:");
display(ar);//1 3 3 5 6 8 6 6
var index=[];
var ar2=[];
for(var a=0;a<ar.length;a++)
{
var duplicate=false;
for(var b=0;b<ar.length;b++)
{
if(ar[a]==ar[b]&&a!=b)
{
duplicate=true;
}
}
if(!duplicate)
{
index.push(a);
}
}
for(var a=0;a<index.length;a++)
{
ar[index[a]]=null;
}
for(var a=0;a<ar.length;a++)
{
if(ar[a]!=null)ar2.push(ar[a]);
}
console.log("After:");
display(ar2);//3 3 6 6 6
function display(x)
{
for(var a=0;a<x.length;a++)console.log(x[a]);
}
The fiddle : http://jsfiddle.net/mageek/6AGQ4/
And a shorter version that is as a function :
var ar=[1,3,3,5,6,8,6,6];
function removeUnique(x)
{
var index=[];
var ar2=[];
for(var a=0;a<ar.length;a++)
{
var duplicate=0;
for(var b=0;b<ar.length;b++)if(ar[a]==ar[b]&&a!=b)duplicate=1;
if(!duplicate)index.push(a);
}
for(var a=0;a<index.length;a++)ar[index[a]]=null;
for(var a=0;a<ar.length;a++)if(ar[a]!=null)ar2.push(ar[a]);
return x;
}
ar=removeUnique(ar);
The fiddle : http://jsfiddle.net/mageek/6AGQ4/2
I'd suggest going for something simple.
Create a short script that flags duplicates
Write the formula directly into the cell "=flagDuplicate(C2,C$2:C$10)"
Copy the forumla down the column
Use Spreadsheet's built in QUERY formula to pull the information you need
"=QUERY(A1:E10; "SELECT * WHERE E = TRUE"; 1)"
Here is a simple function to flag duplicates
function flagDuplicate(value, array) {
var duplicateCounter = 0;
for (var i=0; i<array.length; i++){
if (array[i] == value){ // I avoid === in Spreadsheet functions
duplicateCounter++;
}
}
if (duplicateCounter > 1){
return true;
}else{
return false;
}
}
Too many functions on a large table can slow things down. If it becomes a problem, you can always copy and "paste values only" - that will retain the information but remove the functions.
Best of luck.
Note: When I tested this I noticed that can take a while before the spreadsheet recognizes the new custom function (gives error like can't find function FLAGDUPLICATE)
You could also do it using arrays to handle the whole sheet at once :
function removeUnique(){
var col = 2 ; // choose the column you want to check for unique elements
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data=ss.getDataRange().getValues();// get all data
data.sort(function(x,y){
// var xp = Number(x[col]);// use these to sort on numeric values
// var yp = Number(y[col]);
var xp = x[col];// use these for non-numeric values
var yp = y[col];
Logger.log(xp+' '+yp); // just to check the sort is OK
return xp == yp ? 0 : xp < yp ? -1 : 1;// sort on column col numeric ascending
});
var cc=0;
var newdata = new Array();
for(nn=0;nn<data.length-1;++nn){
if(data[nn+1][col]==data[nn][col]||cc>0){
newdata.push(data[nn]);
++cc;
if(cc>1){cc=0}}
}
ss.getDataRange().clearContent(); // clear the sheet
sh.getRange(1,1,newdata.length,newdata[0].length).setValues(newdata);// paste new values sorted and without unique elements
}
EDIT : here is the version that keeps all duplicates (the working one)
function removeUnique(){
var col = 2 ; // choose the column you want to check for unique elements
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data=ss.getDataRange().getValues();// get all data
data.sort(function(x,y){
// var xp = Number(x[col]);// use these to sort on numeric values
// var yp = Number(y[col]);
var xp = x[col];// use these for non-numeric values
var yp = y[col];
Logger.log(xp+' '+yp); // just to check the sort is OK
return xp == yp ? 0 : xp < yp ? -1 : 1;// sort on column col numeric ascending
});
var newdata = new Array();
for(nn=0;nn<data.length-1;++nn){
if(data[nn+1][col]==data[nn][col]){
newdata.push(data[nn]);
}
}
if(data[nn-1][col]==data[nn][col]){newdata.push(data[nn])}
ss.getDataRange().clearContent(); // clear the sheet
sh.getRange(1,1,newdata.length,newdata[0].length).setValues(newdata);// paste new values sorted and without unique elements
}

Categories

Resources