简体   繁体   中英

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? 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. Is there a way for me to do it via script?

图片

EDIT

As I applied the solution of copyTo(destination) , it worked. But, it is only working on one cell. How can I make it work on B5:F6 or other cells?

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. 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 . 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.

Reference

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