簡體   English   中英

跨電子表格(不是庫)共享的Google電子表格腳本

[英]Google Spreadsheet Scripts shared across spreadsheets (not libraries)

我已經做了大量搜索這個問題,我認為問題是所有答案都會導致需要您創建庫的解決方案。 然后,我看到將該庫添加到電子表格的唯一方法是為該電子表格創建一個新腳本並包含它。

我想要的:一堆電子表格,都包含一個主腳本。 每次更新腳本時,它們都會更新以使用最新的腳本。

我有: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個主腳本1個電子表格模板 ,其中包含引用主文件的腳本,然后是您想要的模板副本

現在,當您設置庫引用時,您可以選擇在開發模式下將其連接到電子表格模板中。 這將允許對主腳本中現有函數的任何更改立即影響模板副本(除非首先需要授權)。 如果您這樣做,您可能希望首先在主腳本的副本中測試您的更改。 另一個選項是關閉開發模式,並讓模板副本的用戶手動更新其每個腳本中的庫版本(除非有一個我不知道的自動版本更新系統)。

此設置仍然無法解決向每個模板副本需要引用的主腳本添加全新功能的問題。 也許有人可以評論或提供單獨的答案。

[更新:2015年3月15日]因此,向Chrome網上應用商店發布附加組件可讓您安裝一次附加組件,並將其顯示在我認為OP最初需要的所有表格/文檔/表單中。 推出新版本會更新所有使用它的Google文檔。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM