By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript

Javascript – How to return closest match for an array in Google Sheets Appscript

Admin
Last updated: 2024/02/10 at 3:43 PM
Admin
Share
3 Min Read
How to return closest match for an array in Google Sheets Appscript

Problem:

I am struggling to return the highest count for a range of cells in A:A. The purpose of this code is to check for the value which is col A inside column N:N and return the highest character count in the sequences found. I am able to return the number of characters from the matched sequences just for A418 and any single cell, (can be A1). The issue is that I cannot get it to work for a range. In short I need to change A418 to a range such as this var dataA = sheet.getRange(“A1:A1000”).getValues();

Contents
Problem:Solution:From:To:
function countCommonCharactersInOrder() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var searchString = sheet.getRange("A418").getValues().toString();
  var data = sheet.getRange("N:N").getValues();

  for (var i = 0; i < data.length; i++) {
    var text = data[i][0].toString();
    var commonCount = countCommonCharsInSequences(searchString, text);
    sheet.getRange(i + 1, 19).setValue(commonCount); // Column S (19th column)
  }
}

function countCommonCharsInSequences(str1, str2) {
  var commonCount = 0;
  
  for (var i = 0; i < str1.length; i++) {
    var currentCommonCount = 0;
    
    for (var j = 0; j < str2.length; j++) {
      if (str1.charAt(i) === str2.charAt(j)) {
        currentCommonCount++;
        i++;
      } else {
        if (currentCommonCount > 2) {
          commonCount += currentCommonCount;
        }
        currentCommonCount = 0;
      }
    }
    
    if (currentCommonCount > 2) {
      commonCount += currentCommonCount;
    }
  }
  
  return commonCount;
}

Solution:

From your reply, how about the following modification?

From:

function countCommonCharactersInOrder() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var searchString = sheet.getRange("A418").getValues().toString();
  var data = sheet.getRange("N:N").getValues();

  for (var i = 0; i < data.length; i++) {
    var text = data[i][0].toString();
    var commonCount = countCommonCharsInSequences(searchString, text);
    sheet.getRange(i + 1, 19).setValue(commonCount); // Column S (19th column)
  }
}

To:

function countCommonCharactersInOrder() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var dataA = sheet.getRange("A1:A" + lastRow).getValues();
  var data = sheet.getRange("N1:N" + lastRow).getValues();
  var res = dataA.map(([searchString]) => [Math.max(...data.map(([text]) => countCommonCharsInSequences(searchString, text)))]);
  sheet.getRange(1, 19, res.length).setValues(res); // Column S (19th column)
}
  • By this modification, about each row of “A1:A”, the maximum values from countCommonCharsInSequences(searchString, text) are put into coulmn “S”.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Javascript, creating an element in an array and setting a property to the new element Javascript – Javascript, creating an element in an array and setting a property to the new element
Next Article Can import all THREE.js post processing modules as ES6 modules except OutputPass Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?