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

时间:2013-01-23 05:57:53

标签: google-apps-script google-sheets

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

  

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

     

我拥有的:        所有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;
}

1 个答案:

答案 0 :(得分:5)

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

  

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

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

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

所以,我们有:1 master script1 spreadsheet template,其中有一个引用主文件的脚本,然后是您想要的copies of the template

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

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

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

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

相关问题