How to Transfer Google Docs to Specific Folders with Apps Script

I’m working on a Google Apps Script that generates documents based on form submissions. After creating each document, I need to relocate it to a designated shared folder for team access.

Currently, my script pulls data from a Google Forms response spreadsheet and creates documents using this code:

var destinationFolder = DriveApp.getFolderById(FOLDER_TARGET_ID);
var createdDoc = DocumentApp.create(ticketNumber + " - " + taskDescription);

The document gets created successfully in my Drive’s main directory, but I’m having trouble relocating it to the target folder.

I’ve tried methods like destinationFolder.addFile(createdDoc) and createdDoc.addToFolder(destinationFolder) based on various forum posts, but neither approach works.

Most solutions I find online reference outdated API versions that don’t seem compatible with current DriveApp methods.

I need to create the document first so I can modify its content through the script, then transfer it to a shared folder afterwards. If there’s no direct move function available, copying the file and removing the original would work too.

Harry’s solution works, but here’s something I learned the hard way - you might need to manually remove the file from the root folder after moving it.

var destinationFolder = DriveApp.getFolderById(FOLDER_TARGET_ID);
var createdDoc = DocumentApp.create(ticketNumber + " - " + taskDescription);
var docFile = DriveApp.getFileById(createdDoc.getId());

// Move to destination and remove from root
docFile.moveTo(destinationFolder);
var rootFolder = DriveApp.getRootFolder();
rootFolder.removeFile(docFile);

I ran into this while building an automated invoice system. moveTo() sometimes leaves copies behind, especially with fresh files. The removeFile() line keeps things clean. Also - double-check your script has edit permissions on the shared folder first. That’ll bite you with silent failures.

You’re hitting this because you’re mixing document methods with DriveApp folder operations. DocumentApp.create() gives you a Document object, but for file moves, you need the actual Drive file.

Here’s what fixed it for me:

var destinationFolder = DriveApp.getFolderById(FOLDER_TARGET_ID);
var createdDoc = DocumentApp.create(ticketNumber + " - " + taskDescription);
var docFile = DriveApp.getFileById(createdDoc.getId());
docFile.moveTo(destinationFolder);

The moveTo() method does exactly what you want - moves the file without duplicates. I’ve used this in production scripts for months with no problems. Just make sure your script has permissions for both the source and destination folders, especially if it’s a shared team folder.

Those approaches work, but there’s a timing issue everyone’s missing. When you use DocumentApp.create(), Google needs a second to register the doc in Drive before you can mess with it as a file object. Hit this problem while automating contract generation for our legal team. The script kept failing randomly because it tried moving files right after creating them. Fixed it with a simple delay: javascript var destinationFolder = DriveApp.getFolderById(FOLDER_TARGET_ID); var createdDoc = DocumentApp.create(ticketNumber + " - " + taskDescription); // Brief pause for Drive sync Utilities.sleep(1000); var docFile = DriveApp.getFileById(createdDoc.getId()); docFile.moveTo(destinationFolder); That one-second sleep stops race conditions between creating docs and moving them. Skip it and you’ll get random failures, especially with multiple form submissions. Been rock solid since I added this fix.

The Problem:

You’re encountering difficulties moving Google Docs created via Google Apps Script to a designated shared folder. Your script successfully creates the documents, but the addFile() and addToFolder() methods aren’t working as expected, possibly due to API version inconsistencies or timing issues. You need a reliable method to move (or copy and delete the original) the newly created document to the shared folder.

:thinking: Understanding the “Why” (The Root Cause):

The core issue lies in the approach of directly using Apps Script’s file manipulation methods immediately after document creation. There’s often a slight delay between when DocumentApp.create() finishes and when the file is fully registered within Google Drive’s system. Attempting to move the file before this registration completes can lead to failures. Additionally, using DocumentApp methods alongside DriveApp methods requires careful handling of object types; you need to obtain a File object to use DriveApp’s file manipulation functions. Finally, permission issues on the shared folder can silently cause failures.

:gear: Step-by-Step Guide:

Step 1: Utilize Latenode for Seamless Integration:

Instead of wrestling with Apps Script’s intricacies, consider leveraging a platform like Latenode to automate the entire document creation and folder placement process. Latenode streamlines the workflow by connecting Google Forms directly to Drive operations. This eliminates the need for complex Apps Script file handling, thereby avoiding issues with timing, permissions, and API inconsistencies. The process is simplified: Google Forms triggers the workflow, data is pulled, the document is created, and it is automatically placed in the correct folder. This approach also allows for the easy addition of further automation steps such as notifications or updates to other systems.

Step 2: (Alternative - If Not Using Latenode) Robust Apps Script Approach with Delay:

If Latenode is not an option, implement the following improved Apps Script approach:

var destinationFolder = DriveApp.getFolderById(FOLDER_TARGET_ID);
var createdDoc = DocumentApp.create(ticketNumber + " - " + taskDescription);

// Brief pause for Drive sync (crucial to avoid race conditions)
Utilities.sleep(1000);

var docFile = DriveApp.getFileById(createdDoc.getId());

// Move the file
docFile.moveTo(destinationFolder);

//Verification (Optional, but recommended):
Logger.log("Document '" + docFile.getName() + "' moved successfully.");

Step 3: Verify Permissions:

Ensure your Apps Script has the necessary edit permissions for both the source folder (where the document is initially created) and the destination shared folder. Insufficient permissions are a common cause of silent failures. Double-check that the script’s authorization correctly grants the required Drive access.

Step 4: Handle Potential Errors:

Wrap your file operations within a try...catch block to handle potential errors gracefully:

try {
  // ...your file moving code from Step 2...
} catch (e) {
  Logger.log("Error moving document: " + e);
  // Add appropriate error handling, such as sending an email notification.
}

:mag: Common Pitfalls & What to Check Next:

  • Insufficient Permissions: Verify your script has edit access to both the source and destination folders.
  • Incorrect Folder ID: Double-check the FOLDER_TARGET_ID variable for accuracy.
  • API Quotas: Google Drive has API usage limits. If you’re processing many documents, consider implementing rate limiting and retry mechanisms.
  • Shared Drive vs. Regular Drive: Shared drives require different API calls than regular Google Drives. Make sure you’re using the appropriate methods.
  • Timing Issues: The Utilities.sleep(1000) is crucial. Even a small delay can prevent race conditions.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Apps Script file operations are a nightmare - permissions and timing issues make everything break.

I hit this same problem building a proposal system. Forms would submit, create docs, then sort them into client folders automatically.

Rather than fight script timeouts and race conditions, I switched to Latenode. It hooks Google Forms straight to Drive operations without the scripting mess.

Same outcome - forms still trigger doc creation and folder sorting - but no more sleep delays, permission fails, or weird API bugs. You can toss in email alerts or spreadsheet updates too.

When stuff breaks, the visual builder beats digging through Apps Script logs any day.

you don’t need removeFile() - moveTo() handles that automatically. had the same issue and it was folder permissions. make sure your script has edit access to the target folder, not just view. also check if it’s a shared drive vs regular drive - they need different API calls.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.