0

Overall goal: Save my pdf in a folder which I define in a google sheets cell

I found this script here where I can save my google slides deck into a pdf which works great.

function pdf(){
var blob = DriveApp.getFileById("### Slide file ID ###").getBlob();
DriveApp.createFile(blob);
}

I would like to define the destination folder ID with referencing a cell in a spreadsheet. I imagine it would be a variation of the code below I found here How to reference a cell as the file or folder id for DriveApp? but I am just really lost now.

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const file_id = ss.getRange("Settings!B9").getValue(); // get value in "Settings!B9"
  const folder_id = ss.getRange("Settings!C9").getValue(); // get value in "Settings!C9"
  const googleDocTemplate = DriveApp.getFileById(file_id);
  const destinationFolder = DriveApp.getFolderById(folder_id)
}

I would have imagined it being something like this but it still saves the pdf into the root folder of my drive

function pdf() {
   const ss = SpreadsheetApp.openById("1j_Ltyx9e8Kb-ywLnJDLl5fzoWpfk3elD9xGvZX665DE");
  const folder_id = ss.getRange("Sheet1!C9").getValue(); // get value in "Sheet1!C9"
  const destinationFolder = DriveApp.getFolderById(folder_id)
  
  var blob = DriveApp.getFileById("1Jac6DZweMjiikCF5qvyZc367PRyn1RoUSs6Osy_F4n0").getBlob();
  DriveApp.createFile(blob);
  
}
Marios
  • 26,333
  • 8
  • 32
  • 52
Mee
  • 133
  • 1
  • 8

1 Answers1

1

You can create the file inside the folder directly:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const file_id = ss.getRange("Settings!B9").getValue(); // get slides id
  const folder_id = ss.getRange("Settings!C9").getValue(); // get folder id
  const folder = DriveApp.getFolderById(folder_id)
  const blob = DriveApp.getFileById(file_id).getBlob();
  folder.createFile(blob); // create pdf file of the slide
}

Make sure file id of the slides is located in Settings!B9 and folder id in Settings!C9.

Marios
  • 26,333
  • 8
  • 32
  • 52
  • Thank you it works for moving files from to the right folder, is there any way of saving down the pdf to a specified folder in the first instance. I am going to have a lot of files saved down and I would prefer not to save it to the root folder of my drive. Thanks! – Mee Feb 20 '21 at 10:05
  • Alternatively if I can automatically retrieve the url of the pdf and inject it into the spreadsheet. Thanks! – Mee Feb 20 '21 at 10:17
  • @Mee Please check my updated code. I used your first approach and make it create the file to specifc folder. Folder id and slides id are given by cells in the spreadsheet. I hope this is the direction you want to follow. – Marios Feb 20 '21 at 10:51
  • 1
    You are amazing! It worked just as I wanted it. You saved me several times now, I really really appreciate it! – Mee Feb 20 '21 at 13:48
  • @Mee I am glad it was helpful to you :) – Marios Feb 20 '21 at 13:50