簡體   English   中英

GAS:在處理程序函數中使用變量時,如何更改全局變量值並保留其更改后的值?

[英]GAS: How to change a global variable value and preserve its changed value when using the variable in a handler function?

我的腳本中有這個全局變量:

var targetDocId = 'No doc ID informed yet';

我試圖在我的主要功能內更改此全局變量的值:

function generatePersonDatasheet() {
  var target = createDuplicateDocument(template, docName);
  var link = target.getUrl();
  targetDocId = target.getId(); // <-------- HERE
  if (theses == 1){
    Logger.log("going to showList() ");
    return showList();
  }   
  showURL(docName, link); 
}

之后,我嘗試在處理程序函數中訪問已更改的全局變量值:

function submit(e){
  var numberOfItems = Number(e.parameter.checkbox_total);
  var thesesArrays = [];
  for(var i = 0; i < numberOfItems; i++){
    if(e.parameter['checkbox_isChecked_'+i] == 'true'){
      thesesArrays.push(fact_list[i]);
    }
  }
  for (var i = 0; i < thesesArrays.length; i++){
    var thesesId = thesesArrays[i][2];
    var thesesType = thesesArrays[i][1];        
    importTheses(targetDocId, thesesId, thesesType); // <-----HERE
  }
  return UiApp.getActiveApplication().close();
}

function importTheses(targetDocId, thesesId, thesesType) {
  var targetDoc = DocumentApp.openById(targetDocId);
  var targetDocParagraphs = targetDoc.getParagraphs();
  var targetDocElements = targetDocParagraphs.getNumChildren();

  var thesesDoc = DocumentApp.openById(thesesId);
  var thesesParagraphs = thesesDoc.getParagraphs();
  var thesesElements = thesesDoc.getNumChildren();

  var eltargetDoc=[];
  var elTheses=[];

  for( var j = 0; j < targetDocElements; ++j ) {
       var targetDocElement = targetDoc.getChild(j);
        eltargetDoc[j]=targetDocElement.getText();
       if(el[j]== thesesType){
           for( var k = 0; k < thesesParagraphs-1; ++k ) {
               var thesesElement = thesesDoc.getChild(k);
               elTheses[k] = thesesDoc.getText();
               targetDoc.insertParagraph(j, elTheses[k]);
         }
      }
   }
}

但是,只要我嘗試更改targetDocId ,當它用作importTheses(targetDocId, thesesId, thesesType);參數時importTheses(targetDocId, thesesId, thesesType); 它仍然具有“尚未通知任何文檔ID”的值,即使我已對其進行了更改,就好像該程序是從頭開始運行的一樣。 是否可以替代此“重置為原始值”行為? 還是我必須使用scriptDB或ScriptProperties存儲全局變量的更改值?

沒有這樣的選擇。 您必須使用scriptProperties或scriptDb。 函數完成后,全局vsriable將超出范圍。 每個外部腳本調用均從零開始。

腳本的每個單獨執行都在新的執行實例中完成。 因此,在代碼塊之外定義的任何變量(也稱為“全局”變量)對於該實例都是唯一的。 當事件觸發一個觸發器函數時,它在自己的實例中運行,並且它設置的任何全局值僅對該實例可見。 例如,由電子表格或文檔UI調用的另一個函數將具有該非范圍對象的自己的版本(全局)。

targetDocId定義和檢索將是Cache Service的良好應用。 注意

function get_targetDocId () {
  var cache = CacheService.getPublicCache();
  var cached = cache.get("targetDocId");
  if (cached != null) {
    return cached;
  }
  var target = createDuplicateDocument(template, docName);  /// Need to add handling for pre-existing document
  var link = target.getUrl();
  var contents = target.getId();
  cache.put("targetDocId", contents, 1500); // cache for 25 minutes
  return contents;
}

現在,不要嘗試使用全局變量,只需調用以下函數:

...
var targetDoc = DocumentApp.openById(get_targetDocId());
...

note緩存服務是可用於Google Apps腳本的永久存儲的一個示例。 在編寫此答案后引入了屬性服務 ,這是一種在執行實例之間持久保存“全局”變量的輕量級方法。


觀察:看來,您使用的是全局(靜態)為tempatedocName ,因為不存在任何參數generatePersonDatasheet() 您可以簡單地動態生成targetDocId。

錯誤:在編寫時,每次需要刷新緩存(15分鍾)時, get_targetDocId()都會創建一個新的docName副本。 您應該添加處理以使文件可能已存在。 (這也適用於您現有的onOpen() 。)

暫無
暫無

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

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