0

I'm trying to export a Google sheet to PDF (that's working) and save it to a Shared Drive. The last part isn't working. I'm able to save it to a Folder on the Google Drive itself, but nog on a Shared Drive.

Probably not the best code ever written, but if you could see beyond my inability to produce something more elegant. I would be thankfull for any good tips!

Kindest regards,

This works

function SaveAsPDF() {
  var sheetname = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var sheetn = SpreadsheetApp.getActive().getSheetByName(sheetname);
  
  const folderName = `typfoldernaam`;
  const nameFile = "test_" + sheetn.getRange('B3').getValue().toString() +".pdf"
  

  DriveApp.getFoldersByName(folderName)
    .next()
    .createFile(SpreadsheetApp.getActiveSpreadsheet()
                  .getBlob()
                  .getAs(`application/pdf`) 
                  .setName(nameFile));
}

Above code writes a PDF to a folder on my Google Drive

The code below doesn't work. It should just make a copy of the current sheet and save it to the shared drive:

function copytoMyDrive(){
  var sApp = SpreadsheetApp.getActiveSpreadsheet();
  var destinationMyDriveId = "ID1234567890";
  var sourcesheet = sApp.getSheetByName("testsheet");
  Drive.Files.copy({title:'Copy of file', parents:[{id:destinationMyDriveId}]}, sourcesheet, {supportsAllDrives: true})

This last one gives me following error: GoogleJsonResponseException: API call to drive.files.copy failed with error: Required

I'm also aware that the last piece of code does not produce a PDF but will simply make a copy. So that would have to be addressed as well...

  • In your question, there are 2 scripts. Which do you want to use by modifying? – Tanaike Apr 18 '23 at 23:03
  • Related question: is this helpful? [App Script for saving a sheet as PDF to a specific folder](https://stackoverflow.com/a/75330467/1330560) – Tedinoz Apr 19 '23 at 03:11
  • @Tanaike: Well, the first one does the job but doesn't save the file to a Shared Drive. I tried to alter it but with no luck. The second part is a new effort to try and save the file to a Shared Drive. I do get to see the name of the drive and Folder, but when I try to copy it to the drive it returns a API call error. For me it doesn't matter how I can make this work, as long as I can understand what is happening and it works. – Stijn Kuppens Apr 19 '23 at 07:52
  • @Tedinoz: This worked. I was always thinking that I couldn't use this because I needed a DriveID instead of the folderID. FolderID was only for the Personal Drive and not the shared one... So I was wrong. This is way more simple in execution as the previous code I used. – Stijn Kuppens Apr 19 '23 at 08:01

0 Answers0