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

旧巷老猫 提交于 2020-12-13 03:29:36

问题


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));
  }
}

回答1:


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

  • Apps Script Event Objects;

  • Apps Script onEdit(e).



来源:https://stackoverflow.com/questions/64998646/google-sheets-add-image-or-drawing-if-a-cell-contains-specific-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!