简体   繁体   English

Google 表格:如果单元格包含特定文本,则添加图像或绘图

[英]Google sheets: Add image or drawing if a cell contains specific text

Just want to ask if how can I add a shape or drawing (half-shaded shape) if the cell has an exact text.只是想问问如果单元格有确切的文本,我如何添加形状或绘图(半阴影形状)。

Is it possible that when I type the letter "t" in the cell, the image/drawing will be visible?当我在单元格中键入字母“t”时,是否可以看到图像/绘图? Though, I don't think it can be done through a formula, given that the cell on which the image/drawing should be placed is the cell on which the letter "t" should be placed.不过,我认为这不能通过公式来完成,因为应放置图像/绘图的单元格是应放置字母“t”的单元格。 Is there a way for me to do it via script?有没有办法让我通过脚本来做到这一点?

图片

EDIT编辑

As I applied the solution of copyTo(destination) , it worked.当我应用copyTo(destination)的解决方案时,它起作用了。 But, it is only working on one cell.但是,它仅适用于一个单元格。 How can I make it work on B5:F6 or other cells?我怎样才能让它在 B5:F6 或其他单元格上工作?

Here are the script and a sample spreadsheet .这是脚本和示例电子表格

function onEdit(e) {
  
  var sheet = SpreadsheetApp.getActiveSheet();
  var r  = sheet.getRange("B5").getValues();
  
  if (r == 't'){
    var rangeToCopy = sheet.getRange(5, 9);
    rangeToCopy.copyTo(sheet.getRange(5, 2));
  }
}

In order to achieve what you want, you should make use of the e event object in order to detect where the edit is being made.为了实现您想要的,您应该使用e事件对象来检测编辑的位置。 Therefore, I suggest you make the following changes to your code:因此,我建议您对代码进行以下更改:

Code代码

function onEdit(e) {
  let sheet = e.range.getSheet();
  let r = e.range.getValue();
  if (sheet.getName() == "Sheet1" && r == "t") {
    let col = e.range.getColumn();
    let row = e.range.getRow();
    sheet.getRange(5,9).copyTo(sheet.getRange(row,col));
  }
}

Explanation解释

The above script checks if the edit has been done in the Sheet1 and if the value of edited cell is t .上面的脚本检查是否在Sheet1完成了编辑,以及编辑的单元格的值是否为t If this condition checks, then by using getRow and getColumn , it gets the range of the cell and then later places the half-shaded shape in that range.如果此条件检查,则通过使用getRowgetColumn ,它获取单元格的范围,然后将半阴影形状放置在该范围内。

Reference参考

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

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