简体   繁体   English

在Google表格中使用fontcolor()返回单元格中的HTML

[英]Using fontcolor() in Google Sheets Returns HTML in Cell

In my sheet, I want to automatically take negative numbers, change the color to red, and remove the '-' character. 在我的工作表中,我想自动取负数,将颜色更改为红色,并删除“-”字符。 The number in a cell can fluctuate between positive and negative, based on data in other cells eg... 单元格中的数字可以根据其他单元格中的数据在正负之间波动,例如...

A1 = "some number"
A2 = "some number"
A3 = A1 + A2 (number should be red if value is a negative, and '-' 
     should be stripped off. Otherwise number should be black.

I've written an apps script that should do this 我写了一个应该执行此操作的应用程序脚本

function negativesToRed(num) {
      if (num < 0) {
        var newNum = (num * -1).toString();
        var redNum = newNum.fontcolor("red");
      }
      return redNum;
    }

but when I use it, it returns... 但是当我使用它时,它返回...

<font color="red">605</font>

...in the cell. ...在牢房中

NOTE: A3 needs to turn red when data inputted in A1 or A2 changes it to a negative. 注意:当在A1或A2中输入的数据更改为负数时,A3需要变为红色。 onEdit() will only work if user manually changes A3 to a negative number. 仅当用户手动将A3更改为负数时,onEdit()才起作用。 AND if the value in A3 is used elsewhere, it needs to be treated as a negative number (removing '-' is just for display). AND如果A3中的值在其他地方使用,则需要将其视为负数(删除“-”仅用于显示)。

Is there anyway to get this to work? 反正有什么要使它起作用?

You can only return value from the custom script function. 您只能从自定义脚本函数返回值。 In order to change the cell color, you can try adding a trigger , such as onEdit , and then apply setFontColor to a cell. 为了更改单元格的颜色,您可以尝试添加触发器 (例如onEdit ,然后将setFontColor应用于单元格。

Yes, there is a way to do this in AppsScript for display purposes. 是的,有一种方法可以在AppsScript中执行此操作以用于显示。 Instead of changing the value of the cell, you can just change the way the number format is displayed. 您可以更改数字格式的显示方式,而无需更改单元格的值。 Set positive numbers to display as black, and negative numbers to display as red without the negative (-) sign showing. 将正数设置为显示为黑色,将负数设置为显示为红色,而不显示负(-)符号。 For example, to change the display of just column 10: 例如,要更改仅列10的显示,请执行以下操作:

var ss = SpreadsheetApp.openById('sheetIDhere').getSheetByName('sheetNameHere');
ss.getRange(2, 10,ss.getLastRow()-1, 1).setNumberFormat('[Black]#,##0;[Red]#,##0;');             

You can also update the number format of the cell/column/sheet by selecting the columns you want to change the view on in the Sheet itself, click on Format -> Number -> More Formats -> Custom Format -> and entering [Black]#,##0;[Red]#,##0; 您还可以通过以下方式更新单元格/列/工作表的数字格式:选择要在工作表本身中更改视图的列,单击格式->数字->更多格式->自定义格式->并输入[黑色]#,## 0; [红色]#,## 0; into the custom area. 进入自定义区域。

By using onEdit you can manipulate the cell, and not only the value. 通过使用onEdit您不仅可以操作值,还可以操作单元格。

Something like this, unless you want to restrict it more. 这样的事情,除非您想进一步限制它。

/** @OnlyCurrentDoc */
function onEdit(e) {
  var range = e.range;
  if (e.value < 0) {
        range.setValue(e.value * -1);
        range.setFontColor('red');
  }
}

onEdit() has limitations and might not trigger if the cell values are changed externally, eg through script execution. onEdit()局限性 ,如果单元格值在外部发生更改(例如通过脚本执行onEdit() ,则可能不会触发。

For this case, consider to install a time-driven trigger instead, which runs eg every minute, if desired. 对于这种情况,可以考虑安装一个time-driven trigger ,如果需要,它可以每分钟运行一次。

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

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