简体   繁体   English

比较Google脚本中的字符串

[英]Comparing strings in google scripts

Trying to make a very basic function in google scripts. 尝试在Google脚本中实现非常基本的功能。 Basically, I want to read a value from cell J5 and increment the value in L5 until J5 says "Good!" 基本上,我想从单元格J5中读取一个值,并在L5中递增该值,直到J5说“好!”。 (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). 我似乎无法测试该字符串..在运行时,代码似乎只是随机停止或永远运行(我还需要代码在显示“ Good!”的那一刻不要触摸电子表格,否则..一切都会得到由于公式再次运行而发生了变化)。

  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. 编辑:正如zbnrg指出的那样,电子表格似乎太慢而无法跟上脚本,这似乎是一个问题。 Can anyone help with code to check a range (A2-A14) for no duplications (ie all unique entries)? 任何人都可以帮助代码检查范围(A2-A14)是否重复(即所有唯一条目)吗?

The recalculation of the "other stuff" in the spreadsheet is slower than the for-loop; 电子表格中“其他内容”的重新计算比for循环要慢; you might pause the calculation and let the spreadsheet recalculate. 您可以暂停计算并让电子表格重新计算。 Within the for-loop include: 在for循环中包括:

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

Javascript: fast, Google Spreadsheet recalc: slow, unpredictable Javascript:快速,Google Spreadsheet重新计算:缓慢,不可预测

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: 使用= rand(),这将生成一个介于0和1之间的随机数:

  1. Insert a column before the names in the A column. 在A列中的名称之前插入一列。
  2. Put the formula =rand() into A2 and drag it the length of the names 将公式= rand()放入A2并将其拖动到名称的长度
  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). 用A列中的值对这两行进行排序。(不管是升序还是降序,因为我们只需要一个随机列表)。
  4. There you go, randomized names. 随机名称随处可见。
  5. Then delete the row of random numbers. 然后删除随机数行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Scripts 比较日期 - Google Scripts comparing dates 将 Google Scripts 中的日期与表格输入进行比较 - Comparing Dates in Google Scripts with Sheets Input Google脚本-Google表单-获取项目字符串 - Google Scripts - Google Forms - Get Strings of Items 比较Google Apps脚本中的两个字符串 - Comparing two strings in Google Apps Script 如何比较 Google App Scripts 中的字符串 - How to compare strings in Google App Scripts Google脚本可以比较在控制台中工作但不在工作表中的两个范围 - Google scripts function comparing two ranges working in console but not in sheets 将Google表格上的日期与Google Apps脚本进行比较会返回不正确的布尔值 - Comparing Dates on Google Sheets with Google Apps Scripts is Returning Inaccurate Boolean Values 使用 Google 表格脚本,为什么我的 if 语句在比较单元格值时总是返回 false? - Using Google Sheets scripts, why does my if statement always return false when comparing cell values? 如何将Google脚本的日期范围字符串(例如“ LAST_MONTH”)转换为实际日期? - How do I convert Google Scripts date range strings like “LAST_MONTH” to actual dates? 使用Google Apps脚本和正则表达式在两个其他字符串之间抓取字符串 - Grabbing string between two other strings using Google Apps Scripts and Regular Expressions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM