簡體   English   中英

form.getResponses()。length返回不一致的響應。

[英]form.getResponses().length returns inconsistent responses.

如果您擁有帶有以下腳本的Google表單,該腳本已附加到電子表格中:

var ss = SpreadsheetApp.openById("INSERT ID");
SpreadsheetApp.setActiveSpreadsheet(ss);//Make it active. 
var sheet = ss.getSheetByName('Form responses 1');
var form = FormApp.openById('INSERT ID');

function testSubmission() {
var numberOfResponses = form.getResponses().length;
var cell = sheet.getRange(sheet.getLastRow(), sheet.getLastColumn());
cell.setValue(numberOfResponses);
}

我希望form.getResponses()。length總是遞增計數。 但是,有時它會跳過一個增量。 例如,您可能會看到1,2,3,3,5,6等。

無論響應距離有多遠,都可能發生這種情況。 在這里查看我的測試回復: 在此處輸入圖片說明

其他人有沒有經歷過? 為什么會這樣? 我懷疑這可能是一個錯誤,但在報告之前需要一些確認。

無法使用您的代碼進行復制,但是它完全錯過了一次發布計數的過程。

考慮:

  • 當您引用的openById()持有腳本時,請使用.getActiveSpreadsheet()而不是openById()
  • 不使用.setActiveSpreadsheet(ss) ,不需要
  • 讓表單保存腳本而不是ss。 盡管與該問題不太可能相關,但onSubmit觸發器可能在那里更可靠。 然后可以使用.getActiveForm()
  • 將響應var與長度分開。 var responses = form.getResponses(); var len = responses.length;
  • 將Form和SS移出全局范圍
  • 使用LockService

暫無
暫無

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

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