google drive | sign a file url for uploading to google storage | create a sheet handler | Search

The copyStudy function duplicates a Google Drive file named "Study sauce template" and grants permissions for a specified email address. It achieves this by listing files, copying the file, inserting permissions, and retrieving a sheet identifier, before returning the new file ID.

Run example

npm run import -- "create a copy of study sauce template"

create a copy of study sauce template

var uuid = require('uuid/v1');
var importer = require('../Core');
var getSheet = importer.import("get sheet identifier");
var copyFile = importer.import("copy a file on google drive");
var listDrive = importer.import("list google drive files");
var insertPermission = importer.import("insert google drive permissions");

function copyStudy(email) {
    var fileId, newId;
    if(!email) {
        throw new Error('email not specified!');
    }
    return listDrive()
        .then(r => fileId = r.filter(f => f.name === 'Study sauce template')[0].id)
        .then(() => copyFile(fileId, 'Study sauce ' + uuid().substr(0, 5)))
        .then(id => newId = id)
        .then(() => insertPermission(newId, email))
        .then(() => getSheet(newId, null, email))
        .then(() => newId)
}

module.exports = copyStudy;

What the code could have been:

// Import required modules
const { v1: uuid } = require('uuid');
const Core = require('../Core');
const { getSheetIdentifier } = Core.import('get sheet identifier');
const { copyFileOnGoogleDrive } = Core.import('copy a file on google drive');
const { listGoogleDriveFiles } = Core.import('list google drive files');
const { insertPermissionOnGoogleDrive } = Core.import('insert google drive permissions');

/**
 * Copies a Google Drive file named 'Study sauce template' to a new file
 * with a name based on the 'Study sauce' prefix and a 5-character uuid.
 * Inserts a permission for the specified email address.
 * Returns the ID of the newly copied file.
 * @param {string} email - Email address for which permission is inserted.
 * @returns {Promise} ID of the newly copied file.
 */
function copyStudy(email) {
  if (!email) {
    throw new Error('Email address not specified!');
  }

  return listGoogleDriveFiles()
   .then((files) => {
      const templateFile = files.find((file) => file.name === 'Study sauce template');
      if (!templateFile) {
        throw new Error('Template file not found!');
      }
      return templateFile.id;
    })
   .then((fileId) => copyFileOnGoogleDrive(fileId, `Study sauce ${uuid().substr(0, 5)}`))
   .then((newId) => {
      return insertPermissionOnGoogleDrive(newId, email)
       .then(() => getSheetIdentifier(newId, null, email))
       .then(() => newId);
    });
}

module.exports = copyStudy;

Breakdown of the Code

Dependencies

Imported Functions

Function copyStudy

Purpose

Duplicates a Google Drive file named "Study sauce template" and inserts permissions for the specified email.

Parameters

Flow

  1. Checks if the email parameter is specified. If not, throws an error.
  2. Lists files in Google Drive using listDrive.
  3. Retrieves the ID of the file named "Study sauce template".
  4. Copies the file using copyFile and generates a new ID.
  5. Inserts permissions for the specified email using insertPermission.
  6. Retrieves the sheet identifier using getSheet.
  7. Returns the new file ID.

Export

The copyStudy function is exported as a module.