【问题标题】:Manipulate google form associated with a google sheet from app script in that sheet从该表中的应用程序脚本操作与谷歌表关联的谷歌表单
【发布时间】:2016-01-11 16:43:20
【问题描述】:

我有一个 google 电子表格,其中包含多个工作表(或选项卡)。每张纸都由其独特的形式填充。电子表格中没有嵌入任何表单。

定期,我需要删除工作表中的所有数据,并删除保存在每个表单中的所有旧响应。我可以使用驻留在电子表格中的 .gs 脚本来执行此操作。它通过其 ID(出现在其 URI 中的长字符串)访问表单。这需要在我的 .gs 脚本中硬编码 ID 字符串。

理想情况下,我想从工作表对象访问每个表单(即每个表单条目的目的地)。模拟代码看起来像这样......

var ss = SpreadSheedApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var form = sheet.getMyAssociatedSourceForm(); // my dream method :-)
form.deleteAllResponses() // this method already exists

有人知道这是否可能吗?还是我必须继续使用该 ID(当前有效)?

rgds...

【问题讨论】:

标签: forms google-apps-script


【解决方案1】:

我认为您无需在脚本中直接输入 ID 即可做到这一点。但是,您需要获取驱动器中的每个表单,遍历所有表单并获取每个表单的 destinationId()

Google Documentation

然后将 destinationId 与当前电子表格 ID 进行比较,无需“硬编码”即可获得:

function deleteAllResponses() {
  var thisSS_ID = SpreadsheetApp.getActiveSpreadsheet().getId();

  var allForms = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
  var thisFormFile, thisFormFileID = "", thisForm, theDestID = "";

  while (allForms.hasNext()) {
    thisFormFile = allForms.next();
    thisFormFileID = thisFormFile.getId();
    thisForm = FormApp.openById(thisFormFileID);

    try {
      theDestID = thisForm.getDestinationId();
    } catch(err) {
      continue;
    };

    if (theDestID === "" || theDestID === undefined) {
      continue;
    };

    if (theDestID === thisFormFileID) {
      thisForm.deleteAllResponses();
    };
  };
};

我没有测试过,所以不知道它是否有效。如果有,请在 cmets 部分告诉我。

【讨论】:

  • 感谢您的回复,您为我指明了解决方案。您不能直接在上面返回的数组/迭代器中的对象上调用 deleteAllResponses()。您需要获取对象的 ID (.gitId()) 并使用 FormApp.openById() 返回打开的表单。然后,您可以同时调用 getDestinationId()deleteAllResponses()。顺便说一句,您还需要捕获周围没有分配目的地的任何表单,否则此错误将停止您的脚本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多