1

I'm trying to write a google apps script for creating a google form quiz based on a question bank contained in a google spreadsheet.

The minimal working example I have coded so far seems to work all right, but I'm not sure my code is entirely correct, let alone optimized. I'll probably share it in a different post later.

Right now I'd like to ask a more general question. I'm not sure I understand completely how "document creation" works in google docs.

My code creates a new form via

var form = FormApp.create('QBANK_1_form');

The actual form is created in the "root" of my google drive. Also, if I run my script multiple times (for debugging purposes), many forms by the same name are created.

Instead, I'd like an unique version of the form to be created in a specific drive folder.

So my question is: what is the correct way to create a form in a chosen drive folder, overwriting a possibly existing file by the same name?

If I get it right, this post seems to suggest that the only way to achieve that is creating the document as above and moving it to the proper folder via the DriveApp.

Is this correct? If so, should I move the document after it is completed or can I do that right after creating it?

Rubén
  • 34,714
  • 9
  • 70
  • 166
PFB
  • 177
  • 2
  • 13

1 Answers1

0
  1. You can move the document right after you create it. The location of the file is of no importance to Google since it 'gets' the file by its ID and not its location or name.

  2. If you create many files in the same folder with the same file name they are all unique files already, although they have the same name and are in the same folder they all have unique file IDs, the file ID is the part in bold: google.com/spreadsheets/d/1E3_g6gUeuv6Mm8RFaq1Rhw7imH55CXOgLO83o5RuszQ/

Or, are you asking that when you create a new file in that folder that is be a different name? i.e. 'QBANK_1_form' then 'QBANK_2_form' etc?

James D
  • 3,102
  • 1
  • 11
  • 21
  • Hi James, thanks. So there is no way of creating a form directly where I want it... Ok. In my script I create the form via `form = FormApp.create('QBANK_1_form');` and then "populate" it with instructions like `item = form.addMultipleChoiceItem()`. Would this still work if I moved the document right after creating it? About the multiple files with the same name: I'd like to avoid them altogether. I guess I should check if there is any, and remove it before creating the new form. Is this correct? Thanks again. – PFB Jul 27 '17 at 14:31
  • Me again. I think I got it. Anyone interested in how I implemented the above suggestion (and in the script I coded for creating a form from a question bank) can take a look at the code at my answer to this [old related post](https://stackoverflow.com/questions/39435580/creating-a-multiple-choice-quiz-from-google-forms/45404610#45404610). Comments are welcome (this is basically my first attempt at using google apps script for creating a form) – PFB Jul 30 '17 at 21:32
  • I think you are thinking of Google docs from a OS point of view where a file is found by its location and name. This is not the case with Drive, a file is located by its ID and its location is of no relevance to the script so you can create the file in the root then move it to wherever you want and it will have no impact on the script whatsoever. You can remove old files by name before or after creating the new file it will make no difference, the order doesn't matter as Google allows to files with the same name. I didn't get to look at your code yet but I will later. – James D Jul 31 '17 at 05:15
  • Hi James, thanks a lot. Yes, I think I'm getting the hang of how drive treats file. – PFB Jul 31 '17 at 08:15
  • Also thanks a lot in advance for taking a look at my script. – PFB Jul 31 '17 at 08:27