Using Google Apps Script, how can I compare the values in one column to the values in other columns?2019 Community Moderator ElectionHow can I upload files asynchronously?How can I merge properties of two JavaScript objects dynamically?How can I convert a string to boolean in JavaScript?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How can I select an element with multiple classes in jQuery?How can I add a key/value pair to a JavaScript object?How can I pretty-print JSON using JavaScript?How can I refresh a page with jQuery?Google Sheets: Dense Ranking from sorted values

Good for you! in Russian

Vocabulary for giving just numbers, not a full answer

How do I express some one as a black person?

How are showroom/display vehicles prepared?

Does "Until when" sound natural for native speakers?

Is compression "encryption" under FCC regs?

Should I take out a loan for a friend to invest on my behalf?

finite abelian groups tensor product.

Hotkey (or other quick way) to insert a keyframe for only one component of a vector-valued property?

How can I get players to stop ignoring or overlooking the plot hooks I'm giving them?

Is it work or heat?

Should I tell my boss the work he did was worthless

meaning and function of 幸 in "则幸分我一杯羹"

Can I pump my MTB tire to max (55 psi / 380 kPa) without the tube inside bursting?

Intuition behind counterexample of Euler's sum of powers conjecture

How can I ensure my trip to the UK will not have to be cancelled because of Brexit?

How to draw cubes in a 3 dimensional plane

Makefile strange variable substitution

When traveling to Europe from North America, do I need to purchase a different power strip?

Database Backup for data and log files

Recommendation letter by significant other if you worked with them professionally?

Child Theme Path Being Ignored With wp_enqueue_scripts

An alternative proof of an application of Hahn-Banach

What problems would a superhuman have whose skin is constantly hot?



Using Google Apps Script, how can I compare the values in one column to the values in other columns?



2019 Community Moderator ElectionHow can I upload files asynchronously?How can I merge properties of two JavaScript objects dynamically?How can I convert a string to boolean in JavaScript?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How can I select an element with multiple classes in jQuery?How can I add a key/value pair to a JavaScript object?How can I pretty-print JSON using JavaScript?How can I refresh a page with jQuery?Google Sheets: Dense Ranking from sorted values










3















So if I have a spreadsheet where people go on and rank songs (each song has it's own row and each user has their own column), how can I write a script to iterate over each user column and then compare the individual user's rating of each song to the column that is the input? The goal is to output the column with rankings most similar to the input column's rankings (ideally this should be an iterative recursive function with space complexity of O(1) and time complexity of O(log(n)).
I would think to make a function that produces a formula to be used like =FINDMOSTSIMILAR(<USER>) that outputs the value of the first row in the output column, but I'm not sure where to begin. I have some experience in JavaScript and I know apps-script is based on JS, but I don't know how to do this function in apps-script.



Thanks!



Spreadsheet: https://docs.google.com/spreadsheets/d/1ruDFa8jmC3okuMvE6n6XfHMU66dNYEfxgUdXl-CMzJo/edit?usp=sharing










share|improve this question


























    3















    So if I have a spreadsheet where people go on and rank songs (each song has it's own row and each user has their own column), how can I write a script to iterate over each user column and then compare the individual user's rating of each song to the column that is the input? The goal is to output the column with rankings most similar to the input column's rankings (ideally this should be an iterative recursive function with space complexity of O(1) and time complexity of O(log(n)).
    I would think to make a function that produces a formula to be used like =FINDMOSTSIMILAR(<USER>) that outputs the value of the first row in the output column, but I'm not sure where to begin. I have some experience in JavaScript and I know apps-script is based on JS, but I don't know how to do this function in apps-script.



    Thanks!



    Spreadsheet: https://docs.google.com/spreadsheets/d/1ruDFa8jmC3okuMvE6n6XfHMU66dNYEfxgUdXl-CMzJo/edit?usp=sharing










    share|improve this question
























      3












      3








      3








      So if I have a spreadsheet where people go on and rank songs (each song has it's own row and each user has their own column), how can I write a script to iterate over each user column and then compare the individual user's rating of each song to the column that is the input? The goal is to output the column with rankings most similar to the input column's rankings (ideally this should be an iterative recursive function with space complexity of O(1) and time complexity of O(log(n)).
      I would think to make a function that produces a formula to be used like =FINDMOSTSIMILAR(<USER>) that outputs the value of the first row in the output column, but I'm not sure where to begin. I have some experience in JavaScript and I know apps-script is based on JS, but I don't know how to do this function in apps-script.



      Thanks!



      Spreadsheet: https://docs.google.com/spreadsheets/d/1ruDFa8jmC3okuMvE6n6XfHMU66dNYEfxgUdXl-CMzJo/edit?usp=sharing










      share|improve this question














      So if I have a spreadsheet where people go on and rank songs (each song has it's own row and each user has their own column), how can I write a script to iterate over each user column and then compare the individual user's rating of each song to the column that is the input? The goal is to output the column with rankings most similar to the input column's rankings (ideally this should be an iterative recursive function with space complexity of O(1) and time complexity of O(log(n)).
      I would think to make a function that produces a formula to be used like =FINDMOSTSIMILAR(<USER>) that outputs the value of the first row in the output column, but I'm not sure where to begin. I have some experience in JavaScript and I know apps-script is based on JS, but I don't know how to do this function in apps-script.



      Thanks!



      Spreadsheet: https://docs.google.com/spreadsheets/d/1ruDFa8jmC3okuMvE6n6XfHMU66dNYEfxgUdXl-CMzJo/edit?usp=sharing







      javascript algorithm google-apps-script google-sheets spreadsheet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 6:27









      JodastJodast

      570221




      570221






















          1 Answer
          1






          active

          oldest

          votes


















          2














          I tried to give you a start. Go to sheet, click Tools > Script Editor. Paste this code.



          To find similar user, what this code does is, between input user and any other user, calculate differences for every song ratings and add them to produce a value. Do this for every user in sheet. Then find the lowest of differences and output which user produces this value. This logic is separated in compare function and you can change it to suit your needs.



          function onOpen() 
          var ui = SpreadsheetApp.getUi();
          // Or DocumentApp or FormApp.
          ui.createMenu('Find Similar User')
          .addItem('Similar User', 'showPrompt')
          .addToUi();


          // this configuration is based on current sheet names and formatting
          // if sheet names change, change needed here
          // if track title and user columns change, change needed here
          var config =
          // sheet name: [ col of track title, col of 1st user, col of last user ]
          "UV6": [6, 9, 28],
          "Midnight Underground": [7, 11, 28],
          "Furious Fap February": [8, 12, 23],
          "March Masturbation Madness": [7, 11, 31]
          ;

          function showPrompt()
          var ui = SpreadsheetApp.getUi(); // Same variations.

          var result = ui.prompt(
          'Find similar user.',
          'Enter user name:',
          ui.ButtonSet.OK_CANCEL);

          // Process the user's response.
          var button = result.getSelectedButton();
          var text = result.getResponseText();
          if (button == ui.Button.OK)
          // User clicked "OK".
          text = text.trim();
          if (!text) return;
          FINDMOSTSIMILAR(text);
          else if (button == ui.Button.CANCEL)

          else if (button == ui.Button.CLOSE)




          function FINDMOSTSIMILAR(username)
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var aSheet = ss.getActiveSheet();
          var aSheetName = aSheet.getName();

          // get positions from config
          if (!config[aSheetName]) return;
          var userNamesColsStart = config[aSheetName][1];
          var userNamesColsSpan = config[aSheetName][2] - config[aSheetName][1] + 1;
          var titleColStart = config[aSheetName][0];

          // read user names
          var users = aSheet.getRange(1,userNamesColsStart,1,userNamesColsSpan).getValues()[0];
          // Logger.log(users);

          // read title col
          var tArr = aSheet.getRange(2,titleColStart,aSheet.getLastRow()-2,1).getValues();
          // Logger.log(tArr);

          // data structure
          var DS = ;
          users.forEach(function(h, i)
          var obj = ;
          var colValues = aSheet.getRange(2,userNamesColsStart+i,aSheet.getLastRow()-2,1).getValues();
          tArr.forEach(function(v, i)
          obj[i] = colValues[i][0];
          );
          DS[h] = obj;
          );
          // Logger.log(DS);

          var target = DS[username];
          delete DS[username];

          var results = [];

          Object.keys(DS).forEach(function(user)
          var obj = ;
          obj.prop = username+'__'+user;
          obj.value = compare(target, DS[user]);
          results.push(obj);
          );

          // sort based on difference values
          results.sort(comp);
          Logger.log(results);

          // user with lowest difference is answer
          var similarUser = results[0].prop.split('__')[1];
          Logger.log(similarUser);

          try
          ss.getActiveCell().setValue([similarUser]);
          catch(e)



          function comp(a, b)
          if (a.value < b.value) return -1;
          else if (a.value > b.value) return 1;
          else return 0;


          // this takes a difference of two users ratings for the same song
          // accumulate all the differences for all songs into a single value
          // change here how to compare 2 song ratings
          function compare(target, user)
          var v = 0;
          Object.keys(target).forEach(function(key, i)
          v += Math.abs(target[key] - user[key]);
          );
          return v;






          share|improve this answer




















          • 1





            Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

            – ra89fi
            Mar 7 at 16:40






          • 1





            Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

            – ra89fi
            Mar 7 at 17:12







          • 1





            that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

            – Jodast
            Mar 7 at 17:20






          • 1





            You're welcome!

            – ra89fi
            Mar 7 at 17:21






          • 1





            Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

            – ra89fi
            Mar 8 at 9:58










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037359%2fusing-google-apps-script-how-can-i-compare-the-values-in-one-column-to-the-valu%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          I tried to give you a start. Go to sheet, click Tools > Script Editor. Paste this code.



          To find similar user, what this code does is, between input user and any other user, calculate differences for every song ratings and add them to produce a value. Do this for every user in sheet. Then find the lowest of differences and output which user produces this value. This logic is separated in compare function and you can change it to suit your needs.



          function onOpen() 
          var ui = SpreadsheetApp.getUi();
          // Or DocumentApp or FormApp.
          ui.createMenu('Find Similar User')
          .addItem('Similar User', 'showPrompt')
          .addToUi();


          // this configuration is based on current sheet names and formatting
          // if sheet names change, change needed here
          // if track title and user columns change, change needed here
          var config =
          // sheet name: [ col of track title, col of 1st user, col of last user ]
          "UV6": [6, 9, 28],
          "Midnight Underground": [7, 11, 28],
          "Furious Fap February": [8, 12, 23],
          "March Masturbation Madness": [7, 11, 31]
          ;

          function showPrompt()
          var ui = SpreadsheetApp.getUi(); // Same variations.

          var result = ui.prompt(
          'Find similar user.',
          'Enter user name:',
          ui.ButtonSet.OK_CANCEL);

          // Process the user's response.
          var button = result.getSelectedButton();
          var text = result.getResponseText();
          if (button == ui.Button.OK)
          // User clicked "OK".
          text = text.trim();
          if (!text) return;
          FINDMOSTSIMILAR(text);
          else if (button == ui.Button.CANCEL)

          else if (button == ui.Button.CLOSE)




          function FINDMOSTSIMILAR(username)
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var aSheet = ss.getActiveSheet();
          var aSheetName = aSheet.getName();

          // get positions from config
          if (!config[aSheetName]) return;
          var userNamesColsStart = config[aSheetName][1];
          var userNamesColsSpan = config[aSheetName][2] - config[aSheetName][1] + 1;
          var titleColStart = config[aSheetName][0];

          // read user names
          var users = aSheet.getRange(1,userNamesColsStart,1,userNamesColsSpan).getValues()[0];
          // Logger.log(users);

          // read title col
          var tArr = aSheet.getRange(2,titleColStart,aSheet.getLastRow()-2,1).getValues();
          // Logger.log(tArr);

          // data structure
          var DS = ;
          users.forEach(function(h, i)
          var obj = ;
          var colValues = aSheet.getRange(2,userNamesColsStart+i,aSheet.getLastRow()-2,1).getValues();
          tArr.forEach(function(v, i)
          obj[i] = colValues[i][0];
          );
          DS[h] = obj;
          );
          // Logger.log(DS);

          var target = DS[username];
          delete DS[username];

          var results = [];

          Object.keys(DS).forEach(function(user)
          var obj = ;
          obj.prop = username+'__'+user;
          obj.value = compare(target, DS[user]);
          results.push(obj);
          );

          // sort based on difference values
          results.sort(comp);
          Logger.log(results);

          // user with lowest difference is answer
          var similarUser = results[0].prop.split('__')[1];
          Logger.log(similarUser);

          try
          ss.getActiveCell().setValue([similarUser]);
          catch(e)



          function comp(a, b)
          if (a.value < b.value) return -1;
          else if (a.value > b.value) return 1;
          else return 0;


          // this takes a difference of two users ratings for the same song
          // accumulate all the differences for all songs into a single value
          // change here how to compare 2 song ratings
          function compare(target, user)
          var v = 0;
          Object.keys(target).forEach(function(key, i)
          v += Math.abs(target[key] - user[key]);
          );
          return v;






          share|improve this answer




















          • 1





            Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

            – ra89fi
            Mar 7 at 16:40






          • 1





            Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

            – ra89fi
            Mar 7 at 17:12







          • 1





            that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

            – Jodast
            Mar 7 at 17:20






          • 1





            You're welcome!

            – ra89fi
            Mar 7 at 17:21






          • 1





            Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

            – ra89fi
            Mar 8 at 9:58















          2














          I tried to give you a start. Go to sheet, click Tools > Script Editor. Paste this code.



          To find similar user, what this code does is, between input user and any other user, calculate differences for every song ratings and add them to produce a value. Do this for every user in sheet. Then find the lowest of differences and output which user produces this value. This logic is separated in compare function and you can change it to suit your needs.



          function onOpen() 
          var ui = SpreadsheetApp.getUi();
          // Or DocumentApp or FormApp.
          ui.createMenu('Find Similar User')
          .addItem('Similar User', 'showPrompt')
          .addToUi();


          // this configuration is based on current sheet names and formatting
          // if sheet names change, change needed here
          // if track title and user columns change, change needed here
          var config =
          // sheet name: [ col of track title, col of 1st user, col of last user ]
          "UV6": [6, 9, 28],
          "Midnight Underground": [7, 11, 28],
          "Furious Fap February": [8, 12, 23],
          "March Masturbation Madness": [7, 11, 31]
          ;

          function showPrompt()
          var ui = SpreadsheetApp.getUi(); // Same variations.

          var result = ui.prompt(
          'Find similar user.',
          'Enter user name:',
          ui.ButtonSet.OK_CANCEL);

          // Process the user's response.
          var button = result.getSelectedButton();
          var text = result.getResponseText();
          if (button == ui.Button.OK)
          // User clicked "OK".
          text = text.trim();
          if (!text) return;
          FINDMOSTSIMILAR(text);
          else if (button == ui.Button.CANCEL)

          else if (button == ui.Button.CLOSE)




          function FINDMOSTSIMILAR(username)
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var aSheet = ss.getActiveSheet();
          var aSheetName = aSheet.getName();

          // get positions from config
          if (!config[aSheetName]) return;
          var userNamesColsStart = config[aSheetName][1];
          var userNamesColsSpan = config[aSheetName][2] - config[aSheetName][1] + 1;
          var titleColStart = config[aSheetName][0];

          // read user names
          var users = aSheet.getRange(1,userNamesColsStart,1,userNamesColsSpan).getValues()[0];
          // Logger.log(users);

          // read title col
          var tArr = aSheet.getRange(2,titleColStart,aSheet.getLastRow()-2,1).getValues();
          // Logger.log(tArr);

          // data structure
          var DS = ;
          users.forEach(function(h, i)
          var obj = ;
          var colValues = aSheet.getRange(2,userNamesColsStart+i,aSheet.getLastRow()-2,1).getValues();
          tArr.forEach(function(v, i)
          obj[i] = colValues[i][0];
          );
          DS[h] = obj;
          );
          // Logger.log(DS);

          var target = DS[username];
          delete DS[username];

          var results = [];

          Object.keys(DS).forEach(function(user)
          var obj = ;
          obj.prop = username+'__'+user;
          obj.value = compare(target, DS[user]);
          results.push(obj);
          );

          // sort based on difference values
          results.sort(comp);
          Logger.log(results);

          // user with lowest difference is answer
          var similarUser = results[0].prop.split('__')[1];
          Logger.log(similarUser);

          try
          ss.getActiveCell().setValue([similarUser]);
          catch(e)



          function comp(a, b)
          if (a.value < b.value) return -1;
          else if (a.value > b.value) return 1;
          else return 0;


          // this takes a difference of two users ratings for the same song
          // accumulate all the differences for all songs into a single value
          // change here how to compare 2 song ratings
          function compare(target, user)
          var v = 0;
          Object.keys(target).forEach(function(key, i)
          v += Math.abs(target[key] - user[key]);
          );
          return v;






          share|improve this answer




















          • 1





            Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

            – ra89fi
            Mar 7 at 16:40






          • 1





            Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

            – ra89fi
            Mar 7 at 17:12







          • 1





            that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

            – Jodast
            Mar 7 at 17:20






          • 1





            You're welcome!

            – ra89fi
            Mar 7 at 17:21






          • 1





            Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

            – ra89fi
            Mar 8 at 9:58













          2












          2








          2







          I tried to give you a start. Go to sheet, click Tools > Script Editor. Paste this code.



          To find similar user, what this code does is, between input user and any other user, calculate differences for every song ratings and add them to produce a value. Do this for every user in sheet. Then find the lowest of differences and output which user produces this value. This logic is separated in compare function and you can change it to suit your needs.



          function onOpen() 
          var ui = SpreadsheetApp.getUi();
          // Or DocumentApp or FormApp.
          ui.createMenu('Find Similar User')
          .addItem('Similar User', 'showPrompt')
          .addToUi();


          // this configuration is based on current sheet names and formatting
          // if sheet names change, change needed here
          // if track title and user columns change, change needed here
          var config =
          // sheet name: [ col of track title, col of 1st user, col of last user ]
          "UV6": [6, 9, 28],
          "Midnight Underground": [7, 11, 28],
          "Furious Fap February": [8, 12, 23],
          "March Masturbation Madness": [7, 11, 31]
          ;

          function showPrompt()
          var ui = SpreadsheetApp.getUi(); // Same variations.

          var result = ui.prompt(
          'Find similar user.',
          'Enter user name:',
          ui.ButtonSet.OK_CANCEL);

          // Process the user's response.
          var button = result.getSelectedButton();
          var text = result.getResponseText();
          if (button == ui.Button.OK)
          // User clicked "OK".
          text = text.trim();
          if (!text) return;
          FINDMOSTSIMILAR(text);
          else if (button == ui.Button.CANCEL)

          else if (button == ui.Button.CLOSE)




          function FINDMOSTSIMILAR(username)
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var aSheet = ss.getActiveSheet();
          var aSheetName = aSheet.getName();

          // get positions from config
          if (!config[aSheetName]) return;
          var userNamesColsStart = config[aSheetName][1];
          var userNamesColsSpan = config[aSheetName][2] - config[aSheetName][1] + 1;
          var titleColStart = config[aSheetName][0];

          // read user names
          var users = aSheet.getRange(1,userNamesColsStart,1,userNamesColsSpan).getValues()[0];
          // Logger.log(users);

          // read title col
          var tArr = aSheet.getRange(2,titleColStart,aSheet.getLastRow()-2,1).getValues();
          // Logger.log(tArr);

          // data structure
          var DS = ;
          users.forEach(function(h, i)
          var obj = ;
          var colValues = aSheet.getRange(2,userNamesColsStart+i,aSheet.getLastRow()-2,1).getValues();
          tArr.forEach(function(v, i)
          obj[i] = colValues[i][0];
          );
          DS[h] = obj;
          );
          // Logger.log(DS);

          var target = DS[username];
          delete DS[username];

          var results = [];

          Object.keys(DS).forEach(function(user)
          var obj = ;
          obj.prop = username+'__'+user;
          obj.value = compare(target, DS[user]);
          results.push(obj);
          );

          // sort based on difference values
          results.sort(comp);
          Logger.log(results);

          // user with lowest difference is answer
          var similarUser = results[0].prop.split('__')[1];
          Logger.log(similarUser);

          try
          ss.getActiveCell().setValue([similarUser]);
          catch(e)



          function comp(a, b)
          if (a.value < b.value) return -1;
          else if (a.value > b.value) return 1;
          else return 0;


          // this takes a difference of two users ratings for the same song
          // accumulate all the differences for all songs into a single value
          // change here how to compare 2 song ratings
          function compare(target, user)
          var v = 0;
          Object.keys(target).forEach(function(key, i)
          v += Math.abs(target[key] - user[key]);
          );
          return v;






          share|improve this answer















          I tried to give you a start. Go to sheet, click Tools > Script Editor. Paste this code.



          To find similar user, what this code does is, between input user and any other user, calculate differences for every song ratings and add them to produce a value. Do this for every user in sheet. Then find the lowest of differences and output which user produces this value. This logic is separated in compare function and you can change it to suit your needs.



          function onOpen() 
          var ui = SpreadsheetApp.getUi();
          // Or DocumentApp or FormApp.
          ui.createMenu('Find Similar User')
          .addItem('Similar User', 'showPrompt')
          .addToUi();


          // this configuration is based on current sheet names and formatting
          // if sheet names change, change needed here
          // if track title and user columns change, change needed here
          var config =
          // sheet name: [ col of track title, col of 1st user, col of last user ]
          "UV6": [6, 9, 28],
          "Midnight Underground": [7, 11, 28],
          "Furious Fap February": [8, 12, 23],
          "March Masturbation Madness": [7, 11, 31]
          ;

          function showPrompt()
          var ui = SpreadsheetApp.getUi(); // Same variations.

          var result = ui.prompt(
          'Find similar user.',
          'Enter user name:',
          ui.ButtonSet.OK_CANCEL);

          // Process the user's response.
          var button = result.getSelectedButton();
          var text = result.getResponseText();
          if (button == ui.Button.OK)
          // User clicked "OK".
          text = text.trim();
          if (!text) return;
          FINDMOSTSIMILAR(text);
          else if (button == ui.Button.CANCEL)

          else if (button == ui.Button.CLOSE)




          function FINDMOSTSIMILAR(username)
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var aSheet = ss.getActiveSheet();
          var aSheetName = aSheet.getName();

          // get positions from config
          if (!config[aSheetName]) return;
          var userNamesColsStart = config[aSheetName][1];
          var userNamesColsSpan = config[aSheetName][2] - config[aSheetName][1] + 1;
          var titleColStart = config[aSheetName][0];

          // read user names
          var users = aSheet.getRange(1,userNamesColsStart,1,userNamesColsSpan).getValues()[0];
          // Logger.log(users);

          // read title col
          var tArr = aSheet.getRange(2,titleColStart,aSheet.getLastRow()-2,1).getValues();
          // Logger.log(tArr);

          // data structure
          var DS = ;
          users.forEach(function(h, i)
          var obj = ;
          var colValues = aSheet.getRange(2,userNamesColsStart+i,aSheet.getLastRow()-2,1).getValues();
          tArr.forEach(function(v, i)
          obj[i] = colValues[i][0];
          );
          DS[h] = obj;
          );
          // Logger.log(DS);

          var target = DS[username];
          delete DS[username];

          var results = [];

          Object.keys(DS).forEach(function(user)
          var obj = ;
          obj.prop = username+'__'+user;
          obj.value = compare(target, DS[user]);
          results.push(obj);
          );

          // sort based on difference values
          results.sort(comp);
          Logger.log(results);

          // user with lowest difference is answer
          var similarUser = results[0].prop.split('__')[1];
          Logger.log(similarUser);

          try
          ss.getActiveCell().setValue([similarUser]);
          catch(e)



          function comp(a, b)
          if (a.value < b.value) return -1;
          else if (a.value > b.value) return 1;
          else return 0;


          // this takes a difference of two users ratings for the same song
          // accumulate all the differences for all songs into a single value
          // change here how to compare 2 song ratings
          function compare(target, user)
          var v = 0;
          Object.keys(target).forEach(function(key, i)
          v += Math.abs(target[key] - user[key]);
          );
          return v;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 21:16

























          answered Mar 7 at 9:02









          ra89fira89fi

          1888




          1888







          • 1





            Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

            – ra89fi
            Mar 7 at 16:40






          • 1





            Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

            – ra89fi
            Mar 7 at 17:12







          • 1





            that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

            – Jodast
            Mar 7 at 17:20






          • 1





            You're welcome!

            – ra89fi
            Mar 7 at 17:21






          • 1





            Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

            – ra89fi
            Mar 8 at 9:58












          • 1





            Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

            – ra89fi
            Mar 7 at 16:40






          • 1





            Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

            – ra89fi
            Mar 7 at 17:12







          • 1





            that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

            – Jodast
            Mar 7 at 17:20






          • 1





            You're welcome!

            – ra89fi
            Mar 7 at 17:21






          • 1





            Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

            – ra89fi
            Mar 8 at 9:58







          1




          1





          Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

          – ra89fi
          Mar 7 at 16:40





          Inside script editor, change FINDMOSTSIMILAR('corbin'); line to put different user names to test. Then go to Run > Run Function > test. How did you test it ?

          – ra89fi
          Mar 7 at 16:40




          1




          1





          Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

          – ra89fi
          Mar 7 at 17:12






          Edited code. Copy, paste and save. Reload sheet tab. Will open a Custom Menu at the right end of menu bar, click Similar User, enter user name, see 'output' sheet.

          – ra89fi
          Mar 7 at 17:12





          1




          1





          that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

          – Jodast
          Mar 7 at 17:20





          that should work, I'll tinker with it a bit to suit my needs, but thanks a ton!

          – Jodast
          Mar 7 at 17:20




          1




          1





          You're welcome!

          – ra89fi
          Mar 7 at 17:21





          You're welcome!

          – ra89fi
          Mar 7 at 17:21




          1




          1





          Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

          – ra89fi
          Mar 8 at 9:58





          Your sheets have different configurations as to where are track titles and user columns. Added a config object based on that and edited code to use new config. Try new code.

          – ra89fi
          Mar 8 at 9:58



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037359%2fusing-google-apps-script-how-can-i-compare-the-values-in-one-column-to-the-valu%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme