Drive API

78 views
Skip to first unread message

Michael Timpano

unread,
Apr 14, 2024, 4:56:18 PMApr 14
to Google Apps Script Community
I am new to using the Google Advanced Services; Drive API v3.

I can seem to save or move the newly created file to the folder I want. No matter what I try they always save in the same folder regardless of the parameter I pass. I also tried Drive.Files.update(but they still moved to the same folder?)?

Thanks in advance for this concern .This is what I have so far:

  while (files.hasNext()) {
    let file = files.next()
    let name = file.getName()

    console.log("Processing file: " + name);


    // name new google sheet file the same as source file
    var newFileData = {
      title: name,
      mimeType: 'application/vnd.google-apps.spreadsheet'
    }
    // create new file in destination folder
    var newFile = Drive.Files.copy(newFileData, file.getId());
    console.log("New file ID: " + newFile.id)

    var newFileId = newFile.id

    DriveApp.getFileById(newFileId).moveTo(destinationFolder)
    DriveApp.getFileById(file.getId()).moveTo(processedFolder) I

Ed Robinson

unread,
Apr 14, 2024, 6:53:41 PMApr 14
to Google Apps Script Community
Hi Michael,
the code below should do what you're looking for.

1. In the root folder of your My Drive, create three folders: InFolder, ProcessedFolder, DestinationFolder
2. Place the files to be processed in the InFolder folder
3. Run the code, it will make a copy of each file, then move the original to ProcessedFolder and the copy to DestinationFolder

I'm not sure what was wrong with the original code, it may be b/c you're mixing calls between the Rest API and the DriveApp service.


function test_on_click() {
var inFolder = DriveApp.getRootFolder().getFoldersByName("InFolder").next()
var processedFolder = DriveApp.getRootFolder().getFoldersByName("ProcessedFolder").next()
var destinationFolder = DriveApp.getRootFolder().getFoldersByName("DestinationFolder").next()

var files = inFolder.getFiles()
while (files.hasNext()) {
let file = files.next()
console.log("Processing file: " + file.getName())
var newFile = file.makeCopy(file.getName())
//
// do processing here
//
file.moveTo(processedFolder)
newFile.moveTo(destinationFolder)
}
}

Michael Timpano

unread,
Apr 14, 2024, 10:03:53 PMApr 14
to Google Apps Script Community
@Ed Robinson thanks for your response. I am curious if I used the folder id (global variable) that specifically holds the excel files I want to convert to Google Sheets like the code below if it would also work?

var sourceFolder = DriveApp.getFolderById(EXCEL_SHEETS_FOLDER_ID);
var files = sourceFolder.getFilesByType(MimeType.MICROSOFT_EXCEL) Your approach works, but I am not sure if that is DriveApp or Drive API v3 methods? The reason I moved to Drive API was to convert the excel sheet to a google sheet in a simpler way (so I thought). I am basically trying to make a copy of the excel file and convert to a google sheet and save them in separate folders. Your method makeCopy works but doesn't convert the file. That is why I was trying to use Drive.Files.copy(newFileData,file.getId()) and pass the newFileData;
 var newFileData = {
      title: name,
      mimeType: 'application/vnd.google-apps.spreadsheet'
    }
I also tried parents: [{id: destinationfolder}], but it would always save it where ever the original excel file is saved? I appreciate you taking the time. Thanks. Take care.

Michael Timpano

unread,
Apr 14, 2024, 10:50:05 PMApr 14
to Google Apps Script Community
This works using Drive API v2 (I can't figure it out on V3 since 'insert' is no longer available. Don't know of create or copy would work the same.

while (files.hasNext()) {

    let file = files.next()
    let blob = file.getBlob()

    // name new google sheet file the same as source file
    let newFileData = {
      title: file.getName(),
      parents: [{ id: destinationFolder}],
      mimeType: MimeType.GOOGLE_SHEETS
    }

    // create new file in destination folder
    let newFile = Drive.Files.insert(newFileData, blob)
    let originalFile = file.moveTo(processedFolder)

      console.log("Processed and converted file: " + newFile.title);
  }

 I hope that helps someone trying to do the same thing.

DimuDesigns

unread,
Apr 15, 2024, 8:31:02 AMApr 15
to Google Apps Script Community
Compare the reference documentation for V2 and V3. V2 has an insert endpoint, V3 has a create endpoint instead.

Michael Timpano

unread,
Apr 16, 2024, 8:43:18 AMApr 16
to google-apps-sc...@googlegroups.com
Thanks @DimuDesigns for your response. The links are helpful. From what I have read on forums is that trying to convert an excel spreadsheet to a google sheets spreadsheet and maintain the data structure, that Drive API is an easier way to accomplish this. It seems more involved to do the same using DriveApp. Take care.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/3-BVRhePITY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/31b65966-fcd4-4177-9d0b-11041d96b730n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages