我如何复制&将FORM移动到Google Apps脚本中的文件夹?

时间:2016-02-15 02:53:50

标签: google-apps-script

我知道如何在文件夹中创建文件(或者在谈论电子表格时移动,因为我们无法直接创建它们。或者我们可以吗?

所以我的问题:

1)我在脚本中创建了一个表单,如何将其移动到子文件夹?

我试过了:

var formFile = DriveApp.getFileById(form.getId());
folder.createFile(formFile.getBlob());
DriveApp.removeFile(formFile);

但是我收到一条错误,说我无法从表单创建pdf文件。

2)有没有办法直接在电子表格中创建电子表格?

我发现的全是:

// create the file in the root
var spreadSheet = SpreadsheetApp.create(title + " (Response)");

// get a file (not a spreadsheet)
var ssFile = DriveApp.getFileById(spreadSheet.getId());

// copy the file into the folder
folder.createFile(ssFile.getBlob());

// delete the file from the root folder.
DriveApp.removeFile(ssFile);

必须有一种更简单的方法。

谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用 高级云端硬盘服务 将新表单(或其他类型的)文件直接创建到文件夹中。以前,高级驱动器服务需要先启用才能使用,但现在却没有。

代码

var folderID = 'myFolderID_Here';

var fileResource = {
  title: 'myNewFile',
  "parents": [{'id':folderID}],  //<--By setting this parent ID to the folder's ID, it creates this file in the correct folder.
  mimeType: 'application/vnd.google-apps.form'
};

var newFile = Drive.Files.insert(fileResource);

然后,您可以使用以下命令获取新电子表格文件的ID:

var theID = newFile.getId();
Logger.log('theID: ' + theID);

要启用高级云端硬盘服务,请点击“资源”菜单,然后选择高级Google服务。

如果您希望从表单中获取所有表单响应,则需要使用以下代码:

function getResponsesOutOfForm() {
  var frm = FormApp.openById('My_Form_ID');
  var allResponses = frm.getResponses();
  var arrayOfData = [], rowArray = [], theResponse, formResponse, itemResponses;

  for (var i = 0; i < allResponses.length; i++) {
    formResponse = allResponses[i];
    itemResponses = formResponse.getItemResponses();

    rowArray = []; //Reset inner array

    for (var j = 0; j < itemResponses.length; j++) {
      theResponse = itemResponses[j].getResponse();
      rowArray.push(theResponse);
    };

    arrayOfData.push(rowArray);
  };

  Logger.log(arrayOfData)
  return arrayOfData;
};

答案 1 :(得分:0)

据我了解,需要-

  1. 创建给定Google表单的副本
  2. 创建一个新的电子表格并将其设置为响应的目的地
  3. 为每组Form和Sheet新建一个文件夹,并将它们都放入其中。
function createTestWFSFolder() {

// These variables can be passed as parameters to this function to make it more modular.
    var url = "https://docs.google.com/forms/d/1XJYb6of3fvHGUHFYnvsmEGrCkbIeUHPhHElpD7SJr7I/edit";
    var folderName = "New Google Form - Sheet Pair";
    var formFileName = "New Google Form";
    
    
//  Create a separate folder called "Surveys" for Google Forms being created by this app script - 
//  Checks if "Surveys" folder already exists, if not a new folder is created and the folder's id is grabbed.
    var allFormsFolderId = "";
    if (!(DriveApp.getFoldersByName("Surveys").hasNext())) {
        Logger.log("Forms Folder not found! Creating one.");
        var parentFolder = DriveApp.getFolderById(DriveApp.getRootFolder().getId());
        allFormsFolderId = parentFolder.createFolder("Surveys").getId();
    } else {
        allFormsFolderId = DriveApp.getFoldersByName("Surveys").next().getId();
        Logger.log("WF Surveys Folder found! ", allFormsFolderId);
    }
    
//  Make New Google Form - Sheet pair folder inside "Surveys" Folder
    var newFormSheetFolderId = DriveApp.getFolderById(allFormsFolderId).createFolder(folderName).getId();
    
//   Make a copy of the input Google Form and paste it inside the New Google Form - Sheet pair folder
    var googleFormId = DriveApp.getFileById(getIdFromUrl(url)).makeCopy(DriveApp.getFolderById(newFormSheetFolderId)).getId();
    var googleForm = FormApp.openById(googleFormId);

//  Create a new Spreadsheet, set Google Form responses to it and move it inside the Survey Folder 
    var ss = SpreadsheetApp.create("New Spreadsheet for Google Form").getId();
    DriveApp.getFileById(ss).moveTo(DriveApp.getFolderById(newFormSheetFolderId));
    googleForm.setDestination(FormApp.DestinationType.SPREADSHEET, ss);
}


function getIdFromUrl(url) {
return url.match(/[-\w]{25,}/);
}