0

I'm trying to create a script that reads the text from a cell and converts it into a URL Handle:

Example: This is a test -> this-is-a-test

I´ve created a code that can convert the text just like the example, but im trying to apply this to a column with 20.000+ rows and the sheet gets very slow or crashes.

Is there a way to optimize the code so that it wont crash and take less time to convert?

This is the code that I've been trying to implement.

This function applies the DASH_CASE to the whole column:

function ApplySeperatedateToColumnEsprinet() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Esprinet_Original");
    ss.getRange("AH2").setFormula("=DASH_CASE(E2)")


    var lr = ss.getLastRow();
    var fillDownRange = ss.getRange(2,34,lr-1);
    ss.getRange("AH2").copyTo(fillDownRange);
}

Code that converts the text to a Handle:

function DASH_CASE(str) {
  return str
      .toLowerCase()
    .split(' ').filter(e => e.trim().length).join('-')
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 1
    It looks like you included code from somewhere else. If you're using someone else's work without giving them credit, that constitutes plagiarism, which is not welcome on Stack Exchange. To fix it, [edit] to make sure you do **ALL** the following: 1.Include a [link](/editing-help#links) to the source, 2. Mention the author's name, and 3. [Quote](/editing-help#simple-blockquotes) the copied content. For more details, see [referencing help](/help/referencing) and [this FAQ](https://meta.stackexchange.com/q/160077/343832). – TheMaster Nov 04 '22 at 15:38
  • @TheMaster, if I were to take a script from stackoverflow and use it in my program as is do I have to put some sort of copyright notice in that script? And without a copyright notice isn't it public domain? – TheWizEd Nov 04 '22 at 15:46
  • 1
    @TheWizEd Yes. You need to give credit. See human readable description of the licence [here](https://creativecommons.org/licenses/by-sa/4.0/)(with pictures). Also see [attribution policy](https://meta.stackexchange.com/a/24618) – TheMaster Nov 04 '22 at 15:53
  • If you will not be sharing the script with anyone, nobody will care if you are providing appropriate attribution. If you think that eventually you might share your scripts, i.e. by posting a question here, it will be a good practice to provide proper attribution at the time that you add the code to your script (the Google Apps Script project). – Rubén Nov 04 '22 at 16:06
  • @TheWizEd IANAL, But Ruben's interpretation is on point. For Personal use, attribution is not required.See [Do I always have to attribute the creator of the licensed material?](https://creativecommons.org/faq/#do-i-always-have-to-attribute-the-creator-of-the-licensed-material) – TheMaster Nov 04 '22 at 16:47
  • @helder.silva.mavrolert IANAL, But The author of the code hasn't granted you permission to post their code under a different licence in public. Although Stackoverflow will probably do nothing about this, you're running foul of their licence terms: "All rights reserved" and anyone modifying your script and posting it as a answer also runs the risk of violating the original owner's rights. Just saying... – TheMaster Nov 04 '22 at 17:06
  • 1
    The answer below is perfect. Basically, you'd use `=dash_case(E2:E100)` in the sheet and change the custom function to adapt to arrays. The link below in the answer gives a great example. Some of my samples: https://stackoverflow.com/questions/69947422/use-a-custom-formula-with-arrayformula-for-distances-between-two-columns-of-citi/69949843#69949843 https://stackoverflow.com/questions/57945431/how-to-use-a-custom-function-with-an-arrayformula/57945866#57945866 using `Array.map` – TheMaster Nov 04 '22 at 17:37

2 Answers2

2

As written in the previous answer by @ Rubén, use

=dash_case(E2:E100)

Then, change your custom formula to support arrays with recursion:

function DASH_CASE(arg) {
  return Array.isArray(arg) 
    ? arg.map(el => DASH_CASE(el))
    : String(arg).toLowerCase()
    .split(' ').filter(e => e.trim().length).join('-')
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • I've tried your recommendation but it gives an error when I try and specify a range in the column. Example:(E2:E120) But when I only select a single cell it works. This is the error message: "TypeError: str.toLowerCase is not a function " – helder.silva.mavrolert Nov 04 '22 at 18:01
  • @helder.silva.mavrolert you're sending numbers or dates. Those won't have `toLowerCase()` Send only strings. Alternatively, try `String(arg).toLowerCase.....` – TheMaster Nov 04 '22 at 18:08
  • You're right about the problem being numbers, what I'm trying to convert is a list of product names that have some numbers in the name itself. Is there a way for the code to see if its a number and just skip it or something like that? – helder.silva.mavrolert Nov 04 '22 at 18:20
  • It only gives that error whe I try to identify the full range of the column but if I specify a cell with text and numbers it returns the Handle correctly. PROTECTOR ANTI-SHOCK IK06 LENOVO -> protector-anti-shock-ik06-lenovo – helder.silva.mavrolert Nov 04 '22 at 18:40
  • @helder.silva.mavrolert Read my last comment again. I already told you the code for how to fix it for numbers. – TheMaster Nov 04 '22 at 18:41
  • I did read and added the code that you gave me but it didn't work, should have told you that in the first comment. – helder.silva.mavrolert Nov 04 '22 at 18:57
  • @helder.silva.mavrolert No repro. Works for me. – TheMaster Nov 04 '22 at 19:18
  • So the problem was that I had a copy of my unoptimized code in another App Script page and it was interfering with your code, it works now. Thanks for your help. – helder.silva.mavrolert Nov 05 '22 at 10:19
1

One way to optimize your script is by changing the approach, instead of using a Google Apps Script function for adding an scalar formula (a formula that returns a single value) with a custom function to multiple contiguous cells, make your custom function able to return an two dimisons Array, then use a single formula. https://developers.google.com/apps-script/guides/sheets/functions#optimization has an example of this.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • IMHO, While code only answers are bad, I think no code answers(where code is appropriate) are the just the same - in the opposite end of the same spectrum. [As the top answer in this meta post says](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers), balance is the key. Even if you don't want to post full code, some snippets may help. Of course you're free to post no code answers, just as others are free to post code only answers. – TheMaster Nov 04 '22 at 18:47
  • 1
    @Rubén Thanks for the help, the dcumentation also gave me a better understanding of how to optimize my future App Script. – helder.silva.mavrolert Nov 05 '22 at 10:22