如何显示 Google 表单是打开还是关闭的?

huangapple go评论66阅读模式
英文:

How to display if a Google Form is open or closed?

问题

我有许多正在运行的Google表单(例如,超过50个),它们计划在收到一定数量的回复后关闭。我想在Sheets中创建一个快速的仪表板,显示我表单是开放还是关闭。这将节省我打开每个单独的表单的时间,然后我可以在一个页面上查看所有内容。

我已经搜索过了,但找不到答案,所以不确定是否可能。

提前感谢。

英文:

I have many Google Forms live (e.g. over 50) that are scheduled to close once a certain number of responses have been received. I would like to create a quick dashboard in Sheets that shows me if a form is open or closed. This will save me from opening each individual form and I can then see everything on one page

I have googled this but cannot find an answer so am not sure it is even possible

Thanks in advance

答案1

得分: 1

Google Forms 服务中有一种名为 isAcceptingResponses() 的方法。使用此方法,您可以查看 Google 表单是否接受答案。

您可以在此处阅读更多关于此方法的信息。

以下是如何使用它的示例,如果您已经有了表单的 ID:

function formStatus() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');

  //手动添加表单的ID。
  let form = FormApp.openById('form_ID');
  let form_status = form.isAcceptingResponses();

  //这将在 Google 表格中添加表单的ID和状态
  sheet.appendRow(['form_ID', form_status])
}

它会看起来像这样:

如何显示 Google 表单是打开还是关闭的?

如果您没有 Google 表单的ID,您可以使用以下代码之一列出 Google Drive 中的表单。使用此代码,您可以列出您的 Drive 中的所有 Google 表单并返回其ID。

function search_forms(){
  let form_list = DriveApp.searchFiles('mimeType = "application/vnd.google-apps.form"');
  
  while (form_list.hasNext()) {
    let form_id = form_list.next().getId();
    Logger.log(form_id)
  }
}

然后,您可以使用同样的方法在其他代码中迭代这些ID,就像这样:

function search_forms(){
  let form_list = DriveApp.searchFiles('mimeType = "application/vnd.google-apps.form"');
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  
  while (form_list.hasNext()) {

    //获取 Google Drive 中的文件ID
    let form_id = form_list.next().getId();
    let form = FormApp.openById(form_id);

    //检查表单是否接受答案
    let form_status = form.isAcceptingResponses();

    //这将在 Google 表格中添加表单的ID和状态
    sheet.appendRow([form_id, form_status])
    
  }
}

它将类似于这样:

如何显示 Google 表单是打开还是关闭的?

更新:

如果您已经有一组ID,您可以像这样将它们添加到 Google 表格中:

如何显示 Google 表单是打开还是关闭的?

之后,您可以使用以下代码从 Google 表格中获取所有ID,并查看表单的状态,然后将其放在下一列中:

function formStatus() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');

  //从 Google 表格的 A 列获取ID
  let form_list = sheet.getRange('A2:A').getValues().flat();

  //启动计数器,以便我们可以将状态放在正确的行
  // 我让它从第 2 行开始,因为这是我配置我的数据的方式
  let count = 2

  //筛选并删除所有空值
  form_list = form_list.filter((element) => element !== '');

  form_list.forEach(function form_ID(form_id) {
    
    let form = FormApp.openById(form_id);
        //检查表单是否接受答案
    let form_status = form.isAcceptingResponses();

    //这将在 Google 表格中添加表单的ID和状态
    // 值将添加在 B 列
    sheet.getRange(count,2).setValue(form_status)
    count +=1
  });
}
英文:

There is a method of Google Forms services called isAcceptingResponses(). With this method you can see if a Google form is accepting answers or not.

You can read more information about this method here

Here is a sample on how to use it, if you already have the ID of the form:

function formStatus() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');

  //Manually add the ID of the form. 
  let form = FormApp.openById('form_ID');
  let form_status = form.isAcceptingResponses();

  //this will add the ID of the form and the status in the google Sheet
  sheet.appendRow(['form_ID', form_status])
}

It will look something like this:

如何显示 Google 表单是打开还是关闭的?

If you do not have the IDs of the Google Forms, you can use a function like this one to list the forms in Google Drive. With this code you can list all the Google Forms inside of your Drive and return the ID.

function search_forms(){
  let form_list = DriveApp.searchFiles('mimeType = "application/vnd.google-apps.form"');
  
  while (form_list.hasNext()) {
    let form_id = form_list.next().getId();
    Logger.log(form_id)
  }
}

You can later iterate the IDs using the same methods on the other code, like this.

function search_forms(){
  let form_list = DriveApp.searchFiles('mimeType = "application/vnd.google-apps.form"');
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  
  while (form_list.hasNext()) {

    //gets the file IDs in Google Drive
    let form_id = form_list.next().getId();
    let form = FormApp.openById(form_id);

    // review if the form is accepting answers or not
    let form_status = form.isAcceptingResponses();

    //this will add the ID of the form and the status in the google Sheet
    sheet.appendRow([form_id, form_status])
    
  }
}

And it will look like this:

如何显示 Google 表单是打开还是关闭的?

<h2> Update: </h2>

If you have a list of IDs already at hand, you can added them to a Google Sheet like this:

如何显示 Google 表单是打开还是关闭的?

After that, you can use this code that will get all the IDs from the Google Sheet, and will review the status of the Form, you can later place it in the next column.

function formStatus() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&#39;Sheet1&#39;);

  //gets the IDs from the column A on Google sheets
  let form_list = sheet.getRange(&#39;A2:A&#39;).getValues().flat();

  //start a counter so we can place the status in the correct row
  // I made it start in row 2 since is the way I configure my data
  let count = 2

  //Filter and remove all the empty values
  form_list = form_list.filter((element) =&gt; element !== &#39;&#39;);

  form_list.forEach(function form_ID(form_id) {
    
    let form = FormApp.openById(form_id);
        // review if the form is accepting answers or not
    let form_status = form.isAcceptingResponses();

    //this will add the ID of the form and the status in the Google Sheet
    // the value will be added in column B
    sheet.getRange(count,2).setValue(form_status)
    count +=1
  });
}

huangapple
  • 本文由 发表于 2023年4月13日 17:43:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定