Create Google Doc file directly in folder rather than in Root folder using Google Apps Script












1















I have this Google Apps Script that works fine:



function myFunction(e) {

var name = e.values[1];
var kantoor = e.values[2];

var doc = DocumentApp.create('Sleuteloverdracht ' + name);
var body = doc.getBody();
body.appendParagraph('Sleuteloverdracht ' + name);
body.appendParagraph('Uw naam is '+ name + 'n' + 'U heeft de sleutel van kantoor '+ kantoor);
doc.saveAndClose();

}


It makes a Google Doc with content from a Spreadsheet that is filled with responses from a Form. It works with a trigger, when the form is send the doc is made.



But the problem is that the Google Doc files are placed in My Drive instead of the same folder as the Form and the Spreadsheet.



Edit: the problem in this Create a Google Doc file directly in a Google Drive folder topic is not the same. I already have a Doc, don't want to create one. The existing Doc just needs to move to another file. See answer below for the solution I used.










share|improve this question

























  • Possible duplicate of Create a document in folder

    – tehhowch
    Nov 27 '18 at 16:29











  • Thanks @tehhowch I indeed used something like the answer in that topic.

    – Nina Bakker
    Nov 28 '18 at 20:47
















1















I have this Google Apps Script that works fine:



function myFunction(e) {

var name = e.values[1];
var kantoor = e.values[2];

var doc = DocumentApp.create('Sleuteloverdracht ' + name);
var body = doc.getBody();
body.appendParagraph('Sleuteloverdracht ' + name);
body.appendParagraph('Uw naam is '+ name + 'n' + 'U heeft de sleutel van kantoor '+ kantoor);
doc.saveAndClose();

}


It makes a Google Doc with content from a Spreadsheet that is filled with responses from a Form. It works with a trigger, when the form is send the doc is made.



But the problem is that the Google Doc files are placed in My Drive instead of the same folder as the Form and the Spreadsheet.



Edit: the problem in this Create a Google Doc file directly in a Google Drive folder topic is not the same. I already have a Doc, don't want to create one. The existing Doc just needs to move to another file. See answer below for the solution I used.










share|improve this question

























  • Possible duplicate of Create a document in folder

    – tehhowch
    Nov 27 '18 at 16:29











  • Thanks @tehhowch I indeed used something like the answer in that topic.

    – Nina Bakker
    Nov 28 '18 at 20:47














1












1








1








I have this Google Apps Script that works fine:



function myFunction(e) {

var name = e.values[1];
var kantoor = e.values[2];

var doc = DocumentApp.create('Sleuteloverdracht ' + name);
var body = doc.getBody();
body.appendParagraph('Sleuteloverdracht ' + name);
body.appendParagraph('Uw naam is '+ name + 'n' + 'U heeft de sleutel van kantoor '+ kantoor);
doc.saveAndClose();

}


It makes a Google Doc with content from a Spreadsheet that is filled with responses from a Form. It works with a trigger, when the form is send the doc is made.



But the problem is that the Google Doc files are placed in My Drive instead of the same folder as the Form and the Spreadsheet.



Edit: the problem in this Create a Google Doc file directly in a Google Drive folder topic is not the same. I already have a Doc, don't want to create one. The existing Doc just needs to move to another file. See answer below for the solution I used.










share|improve this question
















I have this Google Apps Script that works fine:



function myFunction(e) {

var name = e.values[1];
var kantoor = e.values[2];

var doc = DocumentApp.create('Sleuteloverdracht ' + name);
var body = doc.getBody();
body.appendParagraph('Sleuteloverdracht ' + name);
body.appendParagraph('Uw naam is '+ name + 'n' + 'U heeft de sleutel van kantoor '+ kantoor);
doc.saveAndClose();

}


It makes a Google Doc with content from a Spreadsheet that is filled with responses from a Form. It works with a trigger, when the form is send the doc is made.



But the problem is that the Google Doc files are placed in My Drive instead of the same folder as the Form and the Spreadsheet.



Edit: the problem in this Create a Google Doc file directly in a Google Drive folder topic is not the same. I already have a Doc, don't want to create one. The existing Doc just needs to move to another file. See answer below for the solution I used.







google-apps-script google-docs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 9:37







Nina Bakker

















asked Nov 27 '18 at 14:05









Nina BakkerNina Bakker

62




62













  • Possible duplicate of Create a document in folder

    – tehhowch
    Nov 27 '18 at 16:29











  • Thanks @tehhowch I indeed used something like the answer in that topic.

    – Nina Bakker
    Nov 28 '18 at 20:47



















  • Possible duplicate of Create a document in folder

    – tehhowch
    Nov 27 '18 at 16:29











  • Thanks @tehhowch I indeed used something like the answer in that topic.

    – Nina Bakker
    Nov 28 '18 at 20:47

















Possible duplicate of Create a document in folder

– tehhowch
Nov 27 '18 at 16:29





Possible duplicate of Create a document in folder

– tehhowch
Nov 27 '18 at 16:29













Thanks @tehhowch I indeed used something like the answer in that topic.

– Nina Bakker
Nov 28 '18 at 20:47





Thanks @tehhowch I indeed used something like the answer in that topic.

– Nina Bakker
Nov 28 '18 at 20:47












2 Answers
2






active

oldest

votes


















0














This is how to create a new Google Doc in a specific folder. When the Doc is created, the mime type must be set.



Note that this code does NOT first create a Doc file, move it, and then delete the file in the Root Drive. There is another popular answer that does that. This code creates a new Google Doc type file directly in a folder.



You will need to use the Advanced Drive API. It must be enabled in two places.




  • From the code editor choose "Resources" and then choose, "Advanced Google services"

  • Click the button for "Drive API" so that it is ON.

  • Click the link: Google Cloud Platform API Dashboard.

  • Search Drive API

  • Choose Google Drive API from search list

  • Enable the Drive API


Code:



function myFunction(e) {
var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
newDocName,rng,sh,ss,ssFile,ssID;

var name = e.values[1];
var kantoor = e.values[2];

newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

rng = e.range;//Get range in the spreadsheet that received the data
sh = rng.getSheet();//Get sheet tab
ss = sh.getParent();//Get spreadsheet
ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
//folder to put the new file in
multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

folder = multipleFolders.next();//Get the folder of the spreadsheet

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

newFile = Drive.Files.insert(fileResource);//Create the new document file
// directly into the same folder as the spreadsheet
ID_of_newFile = newFile.getId();

//Logger.log(ID_of_newFile)

doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

var body = doc.getBody();
body.appendParagraph('Sleuteloverdracht ' + name);
body.appendParagraph('Uw naam is '+ name + 'n' +
'U heeft de sleutel van kantoor '+ kantoor);
doc.saveAndClose();

}





share|improve this answer


























  • Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

    – Nina Bakker
    Nov 28 '18 at 20:46











  • You should post your solution as an answer. You can answer your own question.

    – Sandy Good
    Nov 28 '18 at 21:22



















0














I found the answer myself.



I used this script (it is a combination of two scripts I found online) to find files with the right name and remove them to the destination folder. After that it removes the file in the root. It works with a trigger, the function is triggered every minute because in my case the Form that creates the Doc (that needs to be moved to the destination folder) is used multiple times per hour (multiple Docs are created).



   function SearchFiles() {

var searchFor ='title contains "NAME/LETTER"';
var names =;
var fileIds=;
var files = DriveApp.searchFiles(searchFor);

while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();
fileIds.push(fileId);
var name = file.getName();
names.push(name);


}

var docFile = DriveApp.getFileById(fileId);
DriveApp.getFolderById('FOLDER_ID_DESTINATION_FOLDER').addFile(docFile);
DriveApp.getRootFolder().removeFile(docFile);

}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501489%2fcreate-google-doc-file-directly-in-folder-rather-than-in-root-folder-using-googl%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is how to create a new Google Doc in a specific folder. When the Doc is created, the mime type must be set.



    Note that this code does NOT first create a Doc file, move it, and then delete the file in the Root Drive. There is another popular answer that does that. This code creates a new Google Doc type file directly in a folder.



    You will need to use the Advanced Drive API. It must be enabled in two places.




    • From the code editor choose "Resources" and then choose, "Advanced Google services"

    • Click the button for "Drive API" so that it is ON.

    • Click the link: Google Cloud Platform API Dashboard.

    • Search Drive API

    • Choose Google Drive API from search list

    • Enable the Drive API


    Code:



    function myFunction(e) {
    var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
    newDocName,rng,sh,ss,ssFile,ssID;

    var name = e.values[1];
    var kantoor = e.values[2];

    newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

    rng = e.range;//Get range in the spreadsheet that received the data
    sh = rng.getSheet();//Get sheet tab
    ss = sh.getParent();//Get spreadsheet
    ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

    ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
    //folder to put the new file in
    multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

    folder = multipleFolders.next();//Get the folder of the spreadsheet

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

    newFile = Drive.Files.insert(fileResource);//Create the new document file
    // directly into the same folder as the spreadsheet
    ID_of_newFile = newFile.getId();

    //Logger.log(ID_of_newFile)

    doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

    var body = doc.getBody();
    body.appendParagraph('Sleuteloverdracht ' + name);
    body.appendParagraph('Uw naam is '+ name + 'n' +
    'U heeft de sleutel van kantoor '+ kantoor);
    doc.saveAndClose();

    }





    share|improve this answer


























    • Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

      – Nina Bakker
      Nov 28 '18 at 20:46











    • You should post your solution as an answer. You can answer your own question.

      – Sandy Good
      Nov 28 '18 at 21:22
















    0














    This is how to create a new Google Doc in a specific folder. When the Doc is created, the mime type must be set.



    Note that this code does NOT first create a Doc file, move it, and then delete the file in the Root Drive. There is another popular answer that does that. This code creates a new Google Doc type file directly in a folder.



    You will need to use the Advanced Drive API. It must be enabled in two places.




    • From the code editor choose "Resources" and then choose, "Advanced Google services"

    • Click the button for "Drive API" so that it is ON.

    • Click the link: Google Cloud Platform API Dashboard.

    • Search Drive API

    • Choose Google Drive API from search list

    • Enable the Drive API


    Code:



    function myFunction(e) {
    var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
    newDocName,rng,sh,ss,ssFile,ssID;

    var name = e.values[1];
    var kantoor = e.values[2];

    newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

    rng = e.range;//Get range in the spreadsheet that received the data
    sh = rng.getSheet();//Get sheet tab
    ss = sh.getParent();//Get spreadsheet
    ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

    ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
    //folder to put the new file in
    multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

    folder = multipleFolders.next();//Get the folder of the spreadsheet

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

    newFile = Drive.Files.insert(fileResource);//Create the new document file
    // directly into the same folder as the spreadsheet
    ID_of_newFile = newFile.getId();

    //Logger.log(ID_of_newFile)

    doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

    var body = doc.getBody();
    body.appendParagraph('Sleuteloverdracht ' + name);
    body.appendParagraph('Uw naam is '+ name + 'n' +
    'U heeft de sleutel van kantoor '+ kantoor);
    doc.saveAndClose();

    }





    share|improve this answer


























    • Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

      – Nina Bakker
      Nov 28 '18 at 20:46











    • You should post your solution as an answer. You can answer your own question.

      – Sandy Good
      Nov 28 '18 at 21:22














    0












    0








    0







    This is how to create a new Google Doc in a specific folder. When the Doc is created, the mime type must be set.



    Note that this code does NOT first create a Doc file, move it, and then delete the file in the Root Drive. There is another popular answer that does that. This code creates a new Google Doc type file directly in a folder.



    You will need to use the Advanced Drive API. It must be enabled in two places.




    • From the code editor choose "Resources" and then choose, "Advanced Google services"

    • Click the button for "Drive API" so that it is ON.

    • Click the link: Google Cloud Platform API Dashboard.

    • Search Drive API

    • Choose Google Drive API from search list

    • Enable the Drive API


    Code:



    function myFunction(e) {
    var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
    newDocName,rng,sh,ss,ssFile,ssID;

    var name = e.values[1];
    var kantoor = e.values[2];

    newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

    rng = e.range;//Get range in the spreadsheet that received the data
    sh = rng.getSheet();//Get sheet tab
    ss = sh.getParent();//Get spreadsheet
    ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

    ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
    //folder to put the new file in
    multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

    folder = multipleFolders.next();//Get the folder of the spreadsheet

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

    newFile = Drive.Files.insert(fileResource);//Create the new document file
    // directly into the same folder as the spreadsheet
    ID_of_newFile = newFile.getId();

    //Logger.log(ID_of_newFile)

    doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

    var body = doc.getBody();
    body.appendParagraph('Sleuteloverdracht ' + name);
    body.appendParagraph('Uw naam is '+ name + 'n' +
    'U heeft de sleutel van kantoor '+ kantoor);
    doc.saveAndClose();

    }





    share|improve this answer















    This is how to create a new Google Doc in a specific folder. When the Doc is created, the mime type must be set.



    Note that this code does NOT first create a Doc file, move it, and then delete the file in the Root Drive. There is another popular answer that does that. This code creates a new Google Doc type file directly in a folder.



    You will need to use the Advanced Drive API. It must be enabled in two places.




    • From the code editor choose "Resources" and then choose, "Advanced Google services"

    • Click the button for "Drive API" so that it is ON.

    • Click the link: Google Cloud Platform API Dashboard.

    • Search Drive API

    • Choose Google Drive API from search list

    • Enable the Drive API


    Code:



    function myFunction(e) {
    var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
    newDocName,rng,sh,ss,ssFile,ssID;

    var name = e.values[1];
    var kantoor = e.values[2];

    newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

    rng = e.range;//Get range in the spreadsheet that received the data
    sh = rng.getSheet();//Get sheet tab
    ss = sh.getParent();//Get spreadsheet
    ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

    ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
    //folder to put the new file in
    multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

    folder = multipleFolders.next();//Get the folder of the spreadsheet

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

    newFile = Drive.Files.insert(fileResource);//Create the new document file
    // directly into the same folder as the spreadsheet
    ID_of_newFile = newFile.getId();

    //Logger.log(ID_of_newFile)

    doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

    var body = doc.getBody();
    body.appendParagraph('Sleuteloverdracht ' + name);
    body.appendParagraph('Uw naam is '+ name + 'n' +
    'U heeft de sleutel van kantoor '+ kantoor);
    doc.saveAndClose();

    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 16:47

























    answered Nov 27 '18 at 15:39









    Sandy GoodSandy Good

    20.6k115497




    20.6k115497













    • Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

      – Nina Bakker
      Nov 28 '18 at 20:46











    • You should post your solution as an answer. You can answer your own question.

      – Sandy Good
      Nov 28 '18 at 21:22



















    • Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

      – Nina Bakker
      Nov 28 '18 at 20:46











    • You should post your solution as an answer. You can answer your own question.

      – Sandy Good
      Nov 28 '18 at 21:22

















    Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

    – Nina Bakker
    Nov 28 '18 at 20:46





    Thanks for your help. Unfortunally I could't get your solution to work but I find another way. I made a different script that search for docs with the right name in the root and moves them to the right folder.

    – Nina Bakker
    Nov 28 '18 at 20:46













    You should post your solution as an answer. You can answer your own question.

    – Sandy Good
    Nov 28 '18 at 21:22





    You should post your solution as an answer. You can answer your own question.

    – Sandy Good
    Nov 28 '18 at 21:22













    0














    I found the answer myself.



    I used this script (it is a combination of two scripts I found online) to find files with the right name and remove them to the destination folder. After that it removes the file in the root. It works with a trigger, the function is triggered every minute because in my case the Form that creates the Doc (that needs to be moved to the destination folder) is used multiple times per hour (multiple Docs are created).



       function SearchFiles() {

    var searchFor ='title contains "NAME/LETTER"';
    var names =;
    var fileIds=;
    var files = DriveApp.searchFiles(searchFor);

    while (files.hasNext()) {
    var file = files.next();
    var fileId = file.getId();
    fileIds.push(fileId);
    var name = file.getName();
    names.push(name);


    }

    var docFile = DriveApp.getFileById(fileId);
    DriveApp.getFolderById('FOLDER_ID_DESTINATION_FOLDER').addFile(docFile);
    DriveApp.getRootFolder().removeFile(docFile);

    }





    share|improve this answer




























      0














      I found the answer myself.



      I used this script (it is a combination of two scripts I found online) to find files with the right name and remove them to the destination folder. After that it removes the file in the root. It works with a trigger, the function is triggered every minute because in my case the Form that creates the Doc (that needs to be moved to the destination folder) is used multiple times per hour (multiple Docs are created).



         function SearchFiles() {

      var searchFor ='title contains "NAME/LETTER"';
      var names =;
      var fileIds=;
      var files = DriveApp.searchFiles(searchFor);

      while (files.hasNext()) {
      var file = files.next();
      var fileId = file.getId();
      fileIds.push(fileId);
      var name = file.getName();
      names.push(name);


      }

      var docFile = DriveApp.getFileById(fileId);
      DriveApp.getFolderById('FOLDER_ID_DESTINATION_FOLDER').addFile(docFile);
      DriveApp.getRootFolder().removeFile(docFile);

      }





      share|improve this answer


























        0












        0








        0







        I found the answer myself.



        I used this script (it is a combination of two scripts I found online) to find files with the right name and remove them to the destination folder. After that it removes the file in the root. It works with a trigger, the function is triggered every minute because in my case the Form that creates the Doc (that needs to be moved to the destination folder) is used multiple times per hour (multiple Docs are created).



           function SearchFiles() {

        var searchFor ='title contains "NAME/LETTER"';
        var names =;
        var fileIds=;
        var files = DriveApp.searchFiles(searchFor);

        while (files.hasNext()) {
        var file = files.next();
        var fileId = file.getId();
        fileIds.push(fileId);
        var name = file.getName();
        names.push(name);


        }

        var docFile = DriveApp.getFileById(fileId);
        DriveApp.getFolderById('FOLDER_ID_DESTINATION_FOLDER').addFile(docFile);
        DriveApp.getRootFolder().removeFile(docFile);

        }





        share|improve this answer













        I found the answer myself.



        I used this script (it is a combination of two scripts I found online) to find files with the right name and remove them to the destination folder. After that it removes the file in the root. It works with a trigger, the function is triggered every minute because in my case the Form that creates the Doc (that needs to be moved to the destination folder) is used multiple times per hour (multiple Docs are created).



           function SearchFiles() {

        var searchFor ='title contains "NAME/LETTER"';
        var names =;
        var fileIds=;
        var files = DriveApp.searchFiles(searchFor);

        while (files.hasNext()) {
        var file = files.next();
        var fileId = file.getId();
        fileIds.push(fileId);
        var name = file.getName();
        names.push(name);


        }

        var docFile = DriveApp.getFileById(fileId);
        DriveApp.getFolderById('FOLDER_ID_DESTINATION_FOLDER').addFile(docFile);
        DriveApp.getRootFolder().removeFile(docFile);

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 9:30









        Nina BakkerNina Bakker

        62




        62






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501489%2fcreate-google-doc-file-directly-in-folder-rather-than-in-root-folder-using-googl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)