save as pdf

230 views
Skip to first unread message

Pravenesh B

unread,
Apr 25, 2023, 3:23:33 AM4/25/23
to Google Apps Script Community
Hello,

I need an app script to download the Google sheet in pdf with the invoice number and store the pdf in a specific file in the drive 

DME

unread,
Apr 25, 2023, 7:59:44 AM4/25/23
to google-apps-sc...@googlegroups.com
Hi 

function downloadSheetAsPDF() {
  // Replace the ID below with your Google Sheet ID
  var sheetId = "INSERT_SHEET_ID_HERE";
 
  // Replace the folder ID below with the ID of the folder where you want to store the PDF
  var folderId = "INSERT_FOLDER_ID_HERE";
 
  // Get the active spreadsheet and sheet
  var ss = SpreadsheetApp.openById(sheetId);
  var sheet = ss.getActiveSheet();
 
  // Get the invoice number from the sheet
  var invoiceNumber = sheet.getRange("A1").getValue();
 
  // Create a new PDF file with the invoice number as the name
  var pdfFile = DriveApp.createFile(sheet.getBlob().getAs('application/pdf')).setName(invoiceNumber + '.pdf');
 
  // Move the PDF file to the specified folder
  var folder = DriveApp.getFolderById(folderId);
  folder.addFile(pdfFile);
  DriveApp.getRootFolder().removeFile(pdfFile);
 
  // Log the URL of the PDF file
  Logger.log("PDF URL: " + pdfFile.getUrl());
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To use this script, you'll need to replace INSERT_SHEET_ID_HERE with the ID of your Google Sheet, and INSERT_FOLDER_ID_HERE with the ID of the folder where you want to store the PDF. You can find the ID of a Google Sheet or folder by looking at the URL in your web browser. For example, the ID of a Google Sheet will be in the URL after spreadsheets/d/, and the ID of a folder will be in the URL after folders/.

On Tue, Apr 25, 2023 at 12:53 PM 'Pravenesh B' via Google Apps Script Community <google-apps-sc...@googlegroups.com> wrote:
Hello,

I need an app script to download the Google sheet in pdf with the invoice number and store the pdf in a specific file in the drive 

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, 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/0dcef4c8-a953-4e97-9c30-f6069cc37e62n%40googlegroups.com.

Pravenesh B

unread,
Apr 26, 2023, 12:16:46 AM4/26/23
to google-apps-sc...@googlegroups.com
Hi

while executing this code I am just getting this error, 
Screenshot 2023-04-26 9.44.11 AM.png

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/hVHyfwjXGrQ/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/CADK4hK%3DzwqmYghgK4%2BNgBsqufbUC%2BRB9Ws4WuSEQ97eo5yqRiQ%40mail.gmail.com.


--
LOGO

Pravenesh B
Trainee Engineer - Work Transformation
Econz IT Services Private Limited

8870052988 email prave...@econz.net
INDIA | Europe | Middle East

facebook instagram linkedin Twitter youtube

footer

esamb...@gmail.com

unread,
Apr 26, 2023, 10:37:00 PM4/26/23
to Google Apps Script Community
You can get a blob only from a Google file.  A spreadsheet is a file; a sheet within the spreadsheet is not a file.  So Google Sheets class does not have  getBlob() method.  The Google Spreadsheet class does have a getBlob() method.
Reply all
Reply to author
Forward
0 new messages