Switching Buttons for Google Spreadsheet using Google Apps Script

Gists

These are the sample scripts for achieving the switching buttons for Google Spreadsheet using Google Apps Script. The management of images using Spreadsheet service is growing now. But, in the current stage, in order to achieve the switching buttons, it needs a little ingenuity. In this report, I would like to introduce 4 kinds of the switching buttons.

Pattern 1

In this pattern, the drawing is used as the button.

Switching Buttons for Google Spreadsheet using Google Apps Script

When the drawing is used as the button, in this case, 2 drawings are prepared and both drawings are overwrapped. Each button has the function of button1 and button2, respectively. When a button is clicked, the z index is replaced. By this, the switching button can be achieved. Because in the current stage, the image cannot be directly edited by the Google Apps Script.

Sample script

const button1 = () => switching("button1");
const button2 = () => switching("button2");

function switching(name) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const drawings = sheet.getDrawings();
  drawings.forEach((d) => d.setZIndex(d.getOnAction() == name ? 0 : 1));
  const temp = ss.insertSheet();
  SpreadsheetApp.flush();
  ss.deleteSheet(temp);
  sheet.activate();
}

Note

Pattern 2

In this pattern, the drawing is used as the button.

Switching Buttons for Google Spreadsheet using Google Apps Script

When the drawing is used as the button, in this case, 2 drawings are prepared, and one button is put to the cell “C3” and another one is other position in the same sheet. Each button has the function of button1 and button2, respectively. When a button is clicked, the clicked button is replaced with the button of the another position. By this, the switching button can be achieved.

Sample script

const button1 = () => switching("button1");
const button2 = () => switching("button2");

function switching(name) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const drawings = sheet.getDrawings();
  if (drawings[0].getOnAction() == name) {
    drawings[1].setPosition(3, 3, 0, 0);
    drawings[0].setPosition(10, 5, 0, 0);
  } else {
    drawings[0].setPosition(3, 3, 0, 0);
    drawings[1].setPosition(10, 5, 0, 0);
  }
}

Note

Pattern 3

In this pattern, the cell is used as the button.

Switching Buttons for Google Spreadsheet using Google Apps Script

When the cell is used as the button, in this case, the cell “C3” is used. And the OnSelectionChange event trigger is used for executing the script.

Sample script

function onSelectionChange(e) {
  const range = e.range;
  const sheet = range.getSheet();
  const a1Notation = range.getA1Notation();
  if (a1Notation == "C3") {
    range.setBackground(
      range.getBackground() == "#0000ff" ? "#ff0000" : "#0000ff"
    );
    sheet.getRange("A1").activate();
  }
}

Note

Pattern 4

In this pattern, the checkbox is used as the button.

Switching Buttons for Google Spreadsheet using Google Apps Script

When the checkbox is used as the button, unfortunately, in this case, the checkbox is put to the cell “C3”. And the OnEdit event trigger is used for executing the script.

Sample script

function onEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  const a1Notation = range.getA1Notation();
  if (a1Notation == "C3") {
    range.setBackground(
      range.getBackground() == "#0000ff" ? "#ff0000" : "#0000ff"
    );
  }
}

Note

Note

References

 Share!