Upload a file to Google Drive using Apps Script

To upload a file to Google Drive using Apps Script, you can use the createFile() method of the DriveApp class. Here’s an example code snippet:

function uploadFileToDrive() {
  var file = DriveApp.createFile('path/to/file.txt', 'File Name'); // Replace with the actual file path and desired name
  
  Logger.log('File uploaded with ID: ' + file.getId());
}

In this code, the uploadFileToDrive function uploads a file to Google Drive. You need to specify the file path and desired name by replacing 'path/to/file.txt' and 'File Name' with the actual values.

The createFile() method is called on the DriveApp object, and the file path and name are passed as arguments. This method creates a new file in the root directory of Google Drive.

The createFile() method returns a File object representing the newly uploaded file. In this example, it’s assigned to the file variable.

To demonstrate the upload, the getId() method is called on the file object to retrieve the unique ID of the uploaded file. This ID is then logged using Logger.log().

You can call the uploadFileToDrive function from another function or set up a trigger to execute it based on your needs. When executed, it will upload the specified file to Google Drive and log its ID.

It’s important to note that the createFile() method will create a new file in Google Drive with the same content as the original file. If you want to upload a file from a web form or user’s device, you’ll need to use the appropriate method to retrieve the file data and create a new file.

Feel free to modify the code to suit your specific requirements, such as uploading files to specific directories or performing additional operations on the uploaded file.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech