Create a google spreadsheet in same folder as current file

爷,独闯天下 提交于 2021-01-27 21:37:33

问题


I have a google form script that I want to create a spreadsheet in the same folder as the form.

  var thisFileId = form.getId();
  var parentFolder = DriveApp.getFolderById(thisFileId)
  
  spreadsheet = SpreadsheetApp.create(spreadsheetName);
  var spreadsheetID = spreadsheet.getId();
  var spreasheetFile = DriveApp.getFileById(spreadsheetID);
  spreasheetFile.moveTo(parentFolder);

I get an error on the last line :

"Exception: Invalid argument: parent.mimeType"


回答1:


I believe your goal as follows.

  • You want to create new Spreadsheet to the same folder with form.getId().

Modification points:

  • In your script, about the variable name from of form.getId(), if form is the Google Form, form.getId() is the file ID of Google form. I think that this might be the reason of your issue.
  • In order to put the new Spreadsheet to the same folder with form, it is required to retrieve the parent folder of form.

When above points are reflected to your script, it becomes as follows.

Modified script:

From:
var parentFolder = DriveApp.getFolderById(thisFileId)
To:
var parentFolder = DriveApp.getFileById(thisFileId).getParents().next();

Note:

  • It seems that when ID except for the folder ID is used for DriveApp.getFolderById(id), no error occurs.

Reference:

  • getParents()


来源:https://stackoverflow.com/questions/64385188/create-a-google-spreadsheet-in-same-folder-as-current-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!