google drive | test google sheet create | merge google drive | Search

The copyFile function copies a file in Google Drive by its ID and assigns a new title, and returns the ID of the copied file. It uses the drive.files.copy method and the authorizeGoogleDrive function to authenticate and perform the file copy operation.

Run example

npm run import -- "copy a file on google drive"

copy a file on google drive

var util = require('util');
var importer = require('../Core');
var authorizeDrive = importer.import("authorize google drive");

function copyFile(fileId, title) {
    return authorizeDrive()
        .then(drive => util.promisify(drive.files.copy.bind(drive))({
            fileId: fileId,
            resource: {name: title},
            convert: true
        }))
        .then(r => r.data.id)
}

module.exports = copyFile;

What the code could have been:

/**
 * Copies a file from Google Drive.
 * 
 * @param {string} fileId - The ID of the file to copy.
 * @param {string} title - The new title of the copied file.
 * @returns {Promise} A promise that resolves with the ID of the copied file.
 */
const { google } = require('googleapis');
const { authorizeGoogleDrive } = require('../Core');

async function copyFile(fileId, title) {
    // Authorize Google Drive to prevent rate limiting and ensure seamless execution.
    const drive = await authorizeGoogleDrive();
    
    // Use the drive.files.copy method to copy the file.
    const result = await drive.files.copy({
        fileId,
        resource: { name: title },
        supportsTeamDrives: true
    });
    
    // Return the ID of the copied file.
    return result.data.id;
}

module.exports = copyFile;

Function Breakdown: copyFile

Parameters

Functionality

Export