Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
JS Adding/Removing image from cell (toggle button)

I am looking for a way to add and remove an image in a cell.

I have the button assigned to this function but I can't seem to add it to the dynamic range. If I hardcode the column and row for testing it adds the image fine but then I can't figure out how to delete it.

function test() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var selection = sheet.getSelection();
  var ranges = selection.getActiveRangeList().getRanges();
  for (var i in ranges) {
    var data = ranges[i].getValues();
    for (var row in data) for (var col in data[row]) {
        var cell = data[row][col];
        if (cell == '') continue;                      // if empty --> go to next cell
        IF CELL CONTAINS IMAGE REMOVE IT
          continue;                                    // --> go to next cell
        } else {
          sheet.insertImage("URL", [col], [row], 125, 2); //Add image at col/row
        }
    }
    ranges[i].setValues(data);
  }
}

Before button click

BEFORE CLICK

After button click

AFTER CLICK

Select new cells

SELECT NEW CELLS

After button click

AFTER BUTTON CLICK

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe your goal is as follows.

  • When the script is run after the cells are selected, you want to put the image on the cell.
  • When the image is not put on the cell, you want to put the image on the cell.
  • When the image has already been put on the cell, you want to remove the image.
  • When the cell value is empty, you don't want to do anything.

In this case, how about the following modified script?

Modified script:

function test() {
  const url = "###"; // Please set your URL.

  const sheet = SpreadsheetApp.getActiveSheet();
  const ranges = sheet.getActiveRangeList().getRanges();
  let images = sheet.getImages();
  ranges.forEach(r => {
    const row = r.getRow();
    const col = r.getColumn();
    const numRows = r.getNumRows();
    const numCols = r.getNumColumns();
    for (let i = 0; i < numRows; i++) {
      for (let j = 0; j < numCols; j++) {
        if (sheet.getRange(row + i, col + j).isBlank()) continue;
        const image = images.filter(e => {
          const anchor = e.getAnchorCell();
          return anchor.getRow() == row + i && anchor.getColumn() == col + j;
        });
        if (image.length > 0) {
          image.forEach(e => e.remove());
          images = sheet.getImages();
        } else {
          sheet.insertImage(url, col + j, row + i, 125, 2);
        }
      }
    }
  });
}
  • At first, please set your URL.
  • When you use this script, please select the cells and run the script.
  • When you run this script for the selected cells, the images are put on the cells which have no images, and the images are removed from the cells which have the images.
  • In this case, the image on the cells can be checked using the method of getAnchorCell() of Class OverGridImage.

References:

  • getImages() of Class Sheet
  • getAnchorCell() of Class OverGridImage
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda