How to create a function that creates a new page in google Sheets

Asked

Viewed 66 times

0

How do I do a function that I can perform in the spreadsheet that generates a page with the given name? Because in the example below I am getting the error: Exception: You do not have permission to call insertSheet

function criarPagina(nome) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.insertSheet(nome);
};
  • The following function works, but only calling the function in the terminal. function criarPagina() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 ss.insertSheet("Nome_daPg");
}; However, I want the same task, but calling it by the spreadsheet.

1 answer

0

Try this:

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu('Extras')
  .addItem('Criar planilha', 'showPrompt')
  .addToUi();
}


function showPrompt() {
  const ui = SpreadsheetApp.getUi();

  const result = ui.prompt(
    'Criar Planilha',
    'Digite o nome da planilha',
    ui.ButtonSet.OK_CANCEL);

  const button = result.getSelectedButton();
  const text = result.getResponseText();
  if (button == ui.Button.OK) {
    let ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.insertSheet(text);
  } else if (button == ui.Button.CANCEL) {

    ui.alert('Por favor digite o nome para a planilha.');
  } else if (button == ui.Button.CLOSE) {
    ui.alert('Alerta fechado.');
  }
}

This will create an "Extras" Menu with a "Create Spreadsheet" sub-menu. A warning will appear to enter the sheet name. Another option would be to add a Drawing and associate a script to it with the name of the script: showPrompt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.