I'm needing to use the Drive.Files.copy
function for copying a file in Team Drives. The functionality is to copy a template Google Doc to a new file and folder.
The function below seems to copy the file, but the resulting file is a PDF (the original file is a Google Doc). It's probably something simple that I'm not seeing.
teacherFolder
is the destination.
learnerDoc
is original file.
newDocc
is new file.
function test() {
var newFile = {
title: "Learner Guide - test",
description: "New student learner guide",
mimetype: 'application/vnd.google-apps.file',
supportsTeamDrives: true,
kind: "drive#user",
includeTeamDriveItems: true
};
// find Teacher's Learner Guides folder
var teacherFolder = DriveApp.getFolderById('1qQJhDMlHZixBO9KZkkoSNYdMuqg0vBPU');
// create duplicate Learner Guide Template document
var learnerDoc = DriveApp.getFileById('1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs');
//var newDocc = Drive.Files.copy(newFile, learnerDoc.getId());
var newDocc = Drive.Files.insert(newFile, learnerDoc.getBlob(), newFile);
var DriveAppFile = DriveApp.getFileById(newDocc.id);
teacherFolder.addFile(DriveAppFile);
Logger.log('file = ' + newDocc.fileExtension);
}
How can I create a duplicate Google Doc in Team Drives and move it to a different folder?
The reason for the "File not found" error is that you are attempting to access a file located in a Team Drive, but do not indicate in the optional parameters that your code knows how to handle the differences between Google Drive and Team Drives.
You have set this parameter, but you set it in the metadata that is associated with the file you are inserting/copying, and not as an optional parameter to the Drive API.
Thus, to resolve the "File not found" error, you need to change the metadata definition:
var newFile = {
title: "Learner Guide - test",
description: "New student learner guide",
mimetype: 'application/vnd.google-apps.file',
supportsTeamDrives: true,
kind: "drive#user",
includeTeamDriveItems: true
};
to metadata and parameters:
const newFile = {
title: "Learner Guide - test",
description: "New student learner guide",
};
const options = {
supportsTeamDrives: true,
includeTeamDriveItems: true
};
I'm not sure what you were trying to do by supplying the mimetype as a generic file (you should let the Drive API infer this for a Copy
operation), or why you try to set the kind
parameter, which is generally a read-only description of the contents of an API response.
With that change, you then pass optional parameters as the last call to the client library method:
var newDocc = Drive.Files.copy(newFile, learnerDoc.getId());
becomes
var newDocc = Drive.Files.copy(newFile, learnerDoc.getId(), options);
Related reading: