如何使用Google Apps Script列出Google表单已发布的URL。

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

How To List Google Form Published URL With Google Apps Script

问题

I can provide the translation for the code portion you provided:

我有一些Google表格在我的驱动器中
我想在电子表格中列出所有这些Google表格的发布网址
为了参考我使用这篇帖子[列出文件][1]并尝试使用[文件类型][2]获取特定类型的文件
当涉及到指定发布网址列表时我遇到了困难
我尝试使用

    function listFormURL(){
    var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MAPPING FORM')
    var folder = DriveApp.getFolderById('1Oj-o5jxxxxxxxxxxxxxxxxxxxJ3fcA'); 
    var list = [];
      list.push(['Name','ID','Size']);
      
    // 我试图有四个列表: list.push(['Name','ID','Size','URL']);    
    // 如何使用下面的变量'formUrl'来获取发布网址列表,而不仅仅是'Name','ID'和'Size'
    var formUrl = FormApp.getActiveForm().getPublishedUrl(); 
    

    var files = folder.getFilesByType(MimeType.GOOGLE_FORMS);
    while (files.hasNext()){
       file = files.next();
       var row = []
       row.push(file.getName(),file.getId(),file.getSize())
      list.push(row);
     }
      sh.getRange(1,1,list.length,list[0].length).setValues(list);
     }

目前我可以获得每个Google表格的'Name''ID''Size'列表

  [1]: https://stackoverflow.com/questions/25360214/list-all-files-id-inside-a-folder-no-subfolders
  [2]: https://developers.google.com/apps-script/reference/base/mime-type

Please note that I've translated the code portion while excluding the non-code parts, as per your request.

英文:

I have some Google Form in my drive.
I want to list all that Google Form published Url in a spreadsheet.
For my reference, I use this post LIST FILES and try to get specific files type with mime type.
I'm having a hard time when it comes to specifying a list of publishedUrl.
I try to use :

function listFormURL(){
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MAPPING FORM')
var folder = DriveApp.getFolderById('1Oj-o5jxxxxxxxxxxxxxxxxxxxJ3fcA'); 
var list = [];
  list.push(['Name','ID','Size']);
  
// I try to have four list :  list.push(['Name','ID','Size','URL']);    
// How to use  variable 'formUrl' below to get published URl list, not just 'Name','ID' and 'Size'
var formUrl = FormApp.getActiveForm().getPublishedUrl(); 


var files = folder.getFilesByType(MimeType.GOOGLE_FORMS);
while (files.hasNext()){
   file = files.next();
   var row = []
   row.push(file.getName(),file.getId(),file.getSize())
  list.push(row);
 }
  sh.getRange(1,1,list.length,list[0].length).setValues(list);
 }

For now, I can get list 'Name','ID'and 'Size' for every Google Form.

答案1

得分: 2

在您的脚本中,以下修改如何?

FormApp.getActiveForm() 用于Google表单的容器绑定脚本。在您的情况下,我认为 FormApp.openById(id) 会更有用。

修改后的脚本:

function listFormURL() {
  var folderId = "###"; // 请设置您的文件夹ID。

  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFilesByType(MimeType.GOOGLE_FORMS);
  var list = [['Name', 'ID', 'Size', 'URL']];
  while (files.hasNext()) {
    var file = files.next();
    var id = file.getId();
    list.push([file.getName(), id, file.getSize(), FormApp.openById(id).getPublishedUrl()]);
  }

  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MAPPING FORM');
  sh.getRange(1, 1, list.length, list[0].length).setValues(list);
}
  • 运行此脚本时,将在 folderId 文件夹下检索Google表单,并从每个Google表单中检索 'Name', 'ID', 'Size', 'URL',然后将这些值放入电子表格中。

参考链接:

英文:

In your script, how about the following modification?

FormApp.getActiveForm() is used for the container-bound script of Google Form. In your situation, I think that FormApp.openById(id) will be useful.

Modified script:

function listFormURL() {
  var folderId = "###"; // Please set your folder ID.

  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFilesByType(MimeType.GOOGLE_FORMS);
  var list = [['Name', 'ID', 'Size', 'URL']];
  while (files.hasNext()) {
    var file = files.next();
    var id = file.getId();
    list.push([file.getName(), id, file.getSize(), FormApp.openById(id).getPublishedUrl()]);
  }

  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MAPPING FORM');
  sh.getRange(1, 1, list.length, list[0].length).setValues(list);
}
  • When this script is run, Google Forms are retrieved just under the folder of folderId, and 'Name', 'ID', 'Size', 'URL' are retrieved from each Google Form, and those values are put to the Spreadsheet.

Reference:

huangapple
  • 本文由 发表于 2023年5月21日 09:41:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297971.html
匿名

发表评论

匿名网友

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

确定