简体   繁体   中英

Comparing strings in google scripts

Trying to make a very basic function in google scripts. Basically, I want to read a value from cell J5 and increment the value in L5 until J5 says "Good!" (which would happen based on other stuff in my spreadsheet). I can't seem to test the string though.. when running, the code just seems to stop randomly or run forever (I also need the code to not touch the spreadsheet the moment it says "Good!", otherwise.. everything gets altered since the formulas run again).

  function makeSelections() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var testcell = ss.getRange('J5')
      var testcellValue = testcell.getValue();
      for (var i = 0; testcellValue != "Good!"; i++) {
        ss.getRange('L5').setValue(i); 
        testcellValue = testcell.getValue();
      }
    }

EDIT: As zbnrg pointed out, it appears to be a problem with the spreadsheet being way too slow to keep up with the script. Can anyone help with code to check a range (A2-A14) for no duplications (ie all unique entries)?

The recalculation of the "other stuff" in the spreadsheet is slower than the for-loop; you might pause the calculation and let the spreadsheet recalculate. Within the for-loop include:

Utilities.sleep(1000); //1000 milliseconds for 1 sec, 2000 for 2, etc.

Javascript: fast, Google Spreadsheet recalc: slow, unpredictable

Update for second part of question

Quick and dirty way to randomize a list without fussing with scripts:

Use =rand(), this generates a random number between 0 and 1:

  1. Insert a column before the names in the A column.
  2. Put the formula =rand() into A2 and drag it the length of the names
  3. Sort both of these rows by the values in column A. (Doesn't matter if it is ascending or descending, since we just want a randomized list).
  4. There you go, randomized names.
  5. Then delete the row of random numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM