在Google Apps脚本中创建附加组件 [英] Creating add ons in Google Apps Script

查看:41
本文介绍了在Google Apps脚本中创建附加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整理了一些我想在其他Google工作表中访问的代码,但是,由于它使用SpreadsheetApp.getUi,因此必须将代码绑定到工作表.因此,我决定将代码创建为附加代码.

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

任何帮助将不胜感激.

谢谢

推荐答案

要在其他文件中使用附加组件,您必须执行以下操作之一:

In order to use the add-on in other files, you would have to do one of the following:

(1)按照(2)通过 Run>测试加载项.测试为附件... .我不建议这样做,因为您必须首先添加要使用附件的每个文件,然后从那里打开文件.

(2) Test the add-on via Run > Test as add-on.... I wouldn't recommend this, since you would have to add each file you want to use the add-on with first, and open the file from there.

一种可能的解决方案,可以达到您的目的,考虑您想要插件的用途,而不是将代码用作插件,而是将其保存为将其包含在您要运行的每个文件中(您必须将 createAddonMenu 更改为 createMenu ).

A possible workaround to reach your purpose would be, considering what you want the add-on for, instead of using your code as an add-on, save it in a library and then include it in each file you want it to run (you would have to change createAddonMenu to createMenu).

这篇关于在Google Apps脚本中创建附加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆