简体   繁体   English

自动将Google表格中单元格内容大写的脚本?

[英]Script to automatically capitalize contents of a cell in Google Sheets?

I have a spreadsheet that takes input of stock symbols. 我有一个电子表格,需要输入股票代码。 I would like them to always be in ALL CAPS regardless of how they are typed in. This appears to require some scripting as there is no way to do this with a function unless a second copy of the column exists, which is not acceptable. 我希望它们始终处于ALL CAPS中,而不管它们的键入方式如何。这似乎需要一些脚本编写,因为除非存在该列的第二个副本,否则无法使用函数来进行此操作,这是不可接受的。

I have a solution which works, with one critical problem. 我有一个有效的解决方案,其中有一个关键问题。 The code is as follows: 代码如下:

function OnEdit(e) {
   var ss = e.source.getActiveSheet(),
       sheets = ['Trades', ''],
       ind = sheets.indexOf(ss.getName());
   if (ind === 0 && e.range.rowStart > 1 && e.range.columnStart >= 1 ) {
       e.range.setValue(e.value.toUpperCase());
   }
}

It works great, and allows me to add as many tabs and columns to format as I wish. 它的效果很好,可以让我添加任意数量的制表符和列以进行格式化。 Unfortunately it also capitalizes the FORMULAS inside the cells, which is breaking formulas that use the importhtml() function, because it capitalizes the URL being requested. 不幸的是,它还会大写单元格内部的importhtml() ,这破坏了使用importhtml()函数的公式,因为它大写了所请求的URL。

So, anyone know a way to do exactly what the above code does, but not touch the actual formulas inside the cells, only the text that they output? 因此,任何人都知道一种可以完全执行上述代码的方法,但不触及单元格内部的实际公式,仅触及它们输出的文本吗?

EDIT: Thanks to @ocordova's comment, I thought I had something that would do the job well enough. 编辑:感谢@ocordova的评论,我认为我有一些可以做得很好的事情。 Unfortunately it's behaving strangely... it works partly o some columns, and not at all on others. 不幸的是,它表现得很奇怪……它在某些列中部分起作用,而在另一些列上则根本不起作用。 Here is my current code (slightly altered from earlier for clarity): 这是我当前的代码(为清晰起见,稍有改动):

function onEdit(e){ 
   var activeSheet = e.source.getActiveSheet(),
       sheets = ['NEW Trades', ''],
       sheetIndex = sheets.indexOf(activeSheet.getName());
  if (sheetIndex === 0 && e.range.rowStart > 1 && e.range.columnStart >0  && e.range.getFormula() == '') {
    e.range.setValue(e.value.toUpperCase());
  }
}

Anyone have any ideas why some cells in some columns will capitalize as expected, and other cells in those same columns won't, and yet other columns won't capitalize at all, anywhere? 有谁知道为什么某些列中的某些单元格会大写,而同一列中的其他单元格却不会大写,而其他列在任何地方都不会大写?

EDIT 2: My trouble appears to be related to, or a conflict with, Data Validation. 编辑2:我的麻烦似乎与数据验证有关或冲突。 The columns I'm trying to capitalize are fed by lists of values on another sheet. 我尝试大写的列由另一张纸上的值列表填充。 If the value was present previously in lower case, and then I applied the data validation to the column, the script will not capitalize the value. 如果该值以前以小写形式存在,然后将数据验证应用于该列,则脚本将不会大写该值。 However if I select the appropriate, capitalized selection from the data validation list, and then re-type the same value in lower case, the script DOES kick in and capitalize. 但是,如果我从数据验证列表中选择适当的大写字母选择,然后以小写形式重新键入相同的值,则脚本会启动并大写。 Very strange and confusing. 非常奇怪和令人困惑。 I could be wrong about the conflict, but that's what it seems like to me. 对于冲突我可能是错的,但这对我来说就是这样。

EDIT 3: It's not related to data validation, because it's behaving the same way on a simple column that has no validation at all. 编辑3:它与数据验证无关,因为它在完全没有验证的简单列上具有相同的方式。 If the value I had previously entered was already in lowercase, then typing it again in lowercase will not activate the script. 如果我先前输入的值已经是小写字母,那么再次以小写字母键入该值将不会激活脚本。 BUT if I type the value in CAPS, then re-type it in lowercase, the script capitalizes it. 但是,如果我在CAPS中键入该值,然后以小写形式重新键入,则脚本将其大写。 Maybe some strange condition relating to when the script is triggered...? 也许与何时触发脚本有关的某些奇怪条件...?

If you don't want to capitalize if the cell contains a formula, you can use the method getFormula() and check if the cell contains a formula. 如果不想大写单元格是否包含公式,则可以使用方法getFormula()并检查单元格是否包含公式。

Returns the formula (A1 notation) for the top-left cell of the range, or an empty string if the cell is empty or doesn't contain a formula . 返回范围左上角单元格的公式(A1表示法); 如果该单元格为空或不包含公式 ,则返回一个空字符串

The code should look like this: 该代码应如下所示:

if (ind === 0 && e.range.rowStart > 1 && e.range.columnStart >= 1  && e.range.getFormula() == '') {
  e.range.setValue(e.value.toUpperCase());
}

EDIT: 编辑:

If I've understood you correctly, you're typing exactly the same value, example: if the value in the cell is México , and you delete all or some characters and inmediately type México again, in that scenario the old value and the new value are the same and the OnEdit() won't be fired. 如果我对您的理解正确,那么您将输入完全相同的值,例如:如果单元格中的值是México ,然后删除所有或某些字符并再次中间键入México ,在这种情况下,旧值和新值值相同,并且不会触发OnEdit() Another example is if you change the format of the value, that's another type of event. 另一个示例是,如果您更改值的格式,则这是另一种事件。

If you want know how the event is considered, you can use an installable on change trigger : 如果您想知道事件的考虑方式,可以在change trigger上使用可安装的

function triggerOnChange(e) {
  MailApp.sendEmail('john.doe@gmail.com', 'Testing triggerOnChange', JSON.stringify(e));
}

Then in the Script Editor menu: Resources -> Current Project Triggers -> Add a new trigger -> ['triggerOnChange', 'From spreadsheet', 'On change'] 然后在“脚本编辑器”菜单中: Resources -> Current Project Triggers -> Add a new trigger -> ['triggerOnChange', 'From spreadsheet', 'On change']

On how to change the case of the formula's result, I think @Rubén has the right idea, but it will only work if the formula contains UPPER() in the first characters, and also since you're using the formula IMPORTHTML() using UPPER() will break it and maybe some other functions like array formulas, unless you use INDEX() : 关于如何更改公式结果的大小写,我认为@Rubén是正确的主意,但只有在公式的第一个字符中包含UPPER()时,并且在使用公式IMPORTHTML()它才有效除非使用INDEX() ,否则UPPER()可能会破坏它,甚至可能破坏数组公式等其他函数:

=INDEX(UPPER(IMPORTHTML(url, query, index)))

Another option could be Regular expressions, but I think it's a little risky considering all the combinations. 另一个选择可能是正则表达式,但考虑到所有组合,我认为这有点冒险。

So, anyone know a way to do exactly what the above code does, but not touch the actual formulas inside the cells, only the text that they output? 因此,任何人都知道一种可以完全执行上述代码的方法,但不触及单元格内部的实际公式,仅触及它们输出的文本吗?

Consider to make a slight change in the OP approach: rather than capitalize all the cells content for any case, capitalize according the following conditions: 考虑对OP方法进行一些更改:在任何情况下,不要大写所有单元格内容,而是大写:

  1. If the cell value, including the values of cells that holds constants or formulas, is not a string then do nothing . 如果单元格值(包括保存常量或公式的单元格的值)不是字符串,则不执行任何操作。
  2. If the cell value is a string 如果单元格值为字符串
    • and the cell content is a constant, then change the case directly from the script. 并且单元格内容是一个常量,然后直接从脚本中更改大小写。
    • and the cell content is a formula, then nest the original formula inside the built-in function UPPER . 并且单元格内容是一个公式,然后将原始公式嵌套在内置函数UPPER中

Example: 例:

function onEdit(e) {
  var range = e.range;
  var value = range.getValue();
  var sheet = range.getSheet();
  var sheetName = sheet.getName();
  if (sheetName === 'Sheet1' && 
      range.getRow() > 1 && 
      range.getColumn() > 1 && 
      typeof value === 'string') {
    if(!range.getFormula()) {
      range.setValue(value.toUpperCase());
    } else {
      if(range.getFormula().substring(0,6).toUpperCase() == '=UPPER') {
        return;
      } else {
        range.setFormula('=UPPER(' + range.getFormula().substring(1) + ')');
      }
    }
  }
}

Notes: 笔记:

  • For simplicity the ind array was not included. 为简单起见,不包括ind数组。
  • typeof e.value always returns 'string' , so instead range.getValue(); typeof e.value始终返回'string' ,所以range.getValue(); is used. 用来。

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

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