I have put together some code which I would like to access in other Google sheets, however, as it uses SpreadsheetApp.getUi the code has to be bound to a sheet. I have therefore decided to create the code as an add on.
Unfortunately, the add ons don't appear in other spreadsheets and disappear from the spreadsheet where the add on was created unless I open up the apps script page. Where am I going wrong?
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem("Delete Columns", "delCols")
.addItem("Insert Columns", "insCols")
.addItem("Subjects Sheet", "deptNamesKS4")
.addItem("Subjects Sheet", "deptNamesKS3")
.addToUi();
};
function onInstall(e) {
onOpen(e);
};
function delCols(e) {
var lastColumn = ss.getLastColumn();
var headers = ss.getRange('1:1').getValues();
var searchVal = ui.prompt("Enter name of column to be deleted").getResponseText()
var names = headers[0];
var loopCounter = names.length - 1
for (var i = loopCounter; i >= 1; i--) {
if(names[i].indexOf(searchVal) > -1) {
ss.deleteColumn(i + 1);
}DE
}
}
function insCols(e) {
var lastColumn = ss.getLastColumn();
var headers = ss.getRange('1:1').getValues();
var searchVal = ui.prompt("Enter name of column to be deleted").getResponseText();
var noCols = ui.prompt("Number of columns to be inserted").getResponseText();
var names = headers[0];
var loopCounter = names.length - 1
for (var i = loopCounter; i >= 1; i--) {
if(names[i].indexOf(searchVal) > -1) {
ss.insertColumnsBefore(i + 1, noCols);
}
}
}
Any help would be appreciated.
Thanks