跨电子表格(不是库)共享的Google电子表格脚本

Joh*_*ews 7 google-sheets google-apps-script

我已经做了大量搜索这个问题,我认为问题是所有答案都会导致需要您创建库的解决方案.然后,我看到将该库添加到电子表格的唯一方法是为该电子表格创建一个新脚本并包含它.

我想要的:一堆电子表格,都包含一个主脚本.每次更新脚本时,它们都会更新以使用最新的脚本.

我有:15个电子表格都有原始脚本的副本.原始脚本已更改,现在看来我必须编辑Copy of myScriptName每个复制的电子表格中存在的每个脚本.

我做了什么:我创建了第一个电子表格,并在我在其脚本编辑器中创建的项目中编写了脚本.工作完美.然后,我为公司的每个部门制作了14份电子表格副本.

如何在任何单个电子表格本身之外共享脚本并进行管理?考虑到所有寻找同样答案的人,我不得不在这里遗漏一些东西.我只是不知道如何使它成为一个库来解决我的用例.

谢谢!

我没有看到这会有什么帮助,但根据评论的要求,脚本是:

function createRollupTable() {

  //Return if:
  //   There is only the account code parameters passed in with no quarterly info
  //   If the length of the account code parameters passed is empty
  if(arguments.length <= 1 || !arguments[0] || arguments[0].length <= 0) {
    return "";
  }

  var rollupTable = new Array();
  var fullListAccountCodes = arguments[0];

  //The first column of output is the full list of account codes for all the quarters
  rollupTable[0] = fullListAccountCodes;

  //Array to keep the YTD total for each account code
  var yearlyAccountCostOutput = new Array(fullListAccountCodes.length);

  //Iterate over all the quarters that were passed in
  for(var i=1;i<arguments.length;i++) {

    //This array should be set to the total length of the available account codes
    var quarterlyRollupCostOutput = new Array(fullListAccountCodes.length);
    var quarterlyBreakdown = arguments[i];
    var quarterIndexCounter = 0;
    var quarterTotalCost = 0;

    //Iterate over all the account codes
    for(var j=0;j<fullListAccountCodes.length && quarterIndexCounter<quarterlyBreakdown.length;j++) {

      //Find the one that matches the current account code for this quarter
      if(fullListAccountCodes[j] == quarterlyBreakdown[quarterIndexCounter][0]) {

        //Set the index of the output based on the full list so they align
        quarterlyRollupCostOutput[j] = quarterlyBreakdown[quarterIndexCounter][1];

        //Add this cost to the running total for the quarter
        quarterTotalCost += quarterlyBreakdown[quarterIndexCounter][1];

        //Add the total amount for the yearly rollup for that account code
        if(yearlyAccountCostOutput[j]) {
          yearlyAccountCostOutput[j] += quarterlyBreakdown[quarterIndexCounter][1];
        } else {
          yearlyAccountCostOutput[j] = quarterlyBreakdown[quarterIndexCounter][1];
        }

        //Increment the counter so we search for the next account code in the quarter
        quarterIndexCounter++;

      }
    }

    rollupTable[i] = quarterlyRollupCostOutput;

    //Add a blank row in the results for spacing
    rollupTable[i].push("");

    //Add the quarterly total cost
    rollupTable[i].push(quarterTotalCost);

  }

  //Add a blank row for spacing
  rollupTable[0].push("");

  //Google spreadsheet forces you to pad with non breaking spaces, no right align option available
  var spaces = "";
  var numSpaces = 66;
  for(var i=0;i<numSpaces;i++){spaces+=String.fromCharCode(160);};

  //Add a row for the Totals
  rollupTable[0].push(spaces + "Totals:");

  //Add the YTD column
  rollupTable.push(yearlyAccountCostOutput);

  return rollupTable;
}
Run Code Online (Sandbox Code Playgroud)

Bry*_*n P 5

也许您要求的只是一种方法,可以将主脚本中的内容完全复制到电子表格副本中的所有脚本中,以便替换代码并跳过引用库的需要,但是我将给出我对图书馆设置如何运作的印象......

我看到将该库添加到电子表格的唯一方法是为该电子表格创建一个新脚本

当您复制已经包含库引用的脚本的电子表格时,它将保留新副本.因此,在创建一个要复制的电子表格模板后,您不必创建任何新脚本.

那个电子表格模板应该有一个对主脚本的库引用.主文件不需要在工作表内,您不应该复制原始/主文件.

因此,我们有:1个主脚本,1个电子表格模板,其中包含引用主文件的脚本,然后是您想要的模板副本.

现在,当您设置库引用时,您可以选择在开发模式下将其连接到电子表格模板中.这将允许对主脚本中现有函数的任何更改立即影响模板副本(除非首先需要授权).如果您这样做,您可能希望首先在主脚本的副本中测试您的更改.另一个选项是关闭开发模式,并让模板副本的用户手动更新其每个脚本中的库版本(除非有一个我不知道的自动版本更新系统).

此设置仍然无法解决向每个模板副本需要引用的主脚本添加全新功能的问题.也许有人可以评论或提供单独的答案.

[更新:2015年3月15日] 因此,向Chrome网上应用商店发布附加组件可让您安装一次附加组件,并将其显示在我认为OP最初需要的所有表格/文档/表单中.推出新版本会更新所有使用它的Google文档.

https://developers.google.com/apps-script/add-ons/