r/GoogleAppsScript • • Nov 25 '20

Question Script pulled ALL of my files rather than just the one.

I have a script that I am having an issue with. The goal is to generate a pdf from a certain sheet in a spreadsheet and then put that PDF into a certain folder.

I am having 2 issues.

  1. When the PDF is generated, the first page of the PDF is blank and the data begins on the second page.

  2. It moved ALL of my files that weren't in a folder into the folder. I can't seem to figure out how to make it pull just that one file.

Any help or guidance would be greatly appreciated.

Here is the script I am using. I pulled it from stack overflow.

function generatePdf(){

//Create a temporary spreadsheet, to store the desired sheet from the spreadsheet in.

var originalSpreadsheet = SpreadsheetApp.getActive();

originalSpreadsheet.setActiveSheet(originalSpreadsheet.getSheets()[4]);

var name = "TEST"+Date()

var newSpreadsheet = SpreadsheetApp.create(name);

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

sheet = originalSpreadsheet.getActiveSheet();

sheet.copyTo(newSpreadsheet);

//Save the desired sheet as pdf

var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf');

var saveCopy = DriveApp.createFile(pdf);

//Delete temporary spreadsheet

DriveApp.getFilesByName(name).next().setTrashed(true);

//Move the pdf file from the rootfolder to the folder where invoices are to be stored.

var files = DriveApp.getRootFolder().getFiles();

while (files.hasNext()) {

var file = files.next();

var destination = DriveApp.getFolderById("123stringofnumbers456");

destination.addFile(file);

var pull = DriveApp.getRootFolder();

pull.removeFile(file);

}

}

7 Upvotes

6 comments sorted by

3

u/catmandx Nov 25 '20

It's because you ARE getting all Files in the root folder.

I suggest using the pdf's fileId to move it to another location.

Just read the line:

var files = DriveApp.getRoot().getFiles()

Now the variable "files" is an array with all the files in it.

You even use a while loop to move all the files to the folder. I suggest trying to understand what the script does before using it.

1

u/triplej158 Nov 25 '20

So how do I make it find just the one file? After getfiles() do I put the name of the file?

1

u/Strel0k Nov 25 '20

You literally are already doing it with

DriveApp.getFilesByName(name).next()

1

u/mshcruz Nov 27 '20

When the PDF is generated, the first page of the PDF is blank and the data begins on the second page.

This happens because the temporary sheet is created with a default sheet in it, so you're exporting that default sheet as well. To solve it, you can delete the first sheet after copying the original to the temporary.

It moved ALL of my files that weren't in a folder into the folder. I can't seem to figure out how to make it pull just that one file.

It's not really necessary to save the PDF to the root folder and then move it, as you can save it directly to the destination folder. Here's the original code with some changes:

function generatePdf(){
  //Create a temporary spreadsheet, to store the desired sheet from the spreadsheet in.
  var name = "TEST"+Date()
  var originalSpreadsheet = SpreadsheetApp.getActiveSheet();
  var newSpreadsheet = SpreadsheetApp.create(name);

  originalSpreadsheet.copyTo(newSpreadsheet);

  //Delete the default sheet ('Sheet1') from the temporary spreadsheet
  newSpreadsheet.deleteSheet(newSpreadsheet.getSheets()[0]);
  
  //Save the desired sheet as pdf
  var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf');
  var saveCopy = DriveApp.getFolderById('123stringofnumbers456').createFile(pdf).setName(name);

  //Delete temporary spreadsheet
  DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
}

1

u/triplej158 Nov 27 '20

Thank you! this is great! Now what is the best way to make it get a specific sheet?

I have tried

originalSpreadsheet.setActiveSheet(originalSpreadsheet.getSheets()[4]) in a few different places and it then seems to screw up a different part of it.

1

u/mshcruz Nov 29 '20

Instead of changing the active sheet, it's probably easier if you get the sheet you want by its name.

So it could be something like the following:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SheetToExport");

Alternatively, if you don't know the name, but know the position, you can use getSheets() and then specify its index. For example, to get the first sheet:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];