0

excuse my bad English.

I have a main Google Sheet that export that from other sheets to fill the information. This action depends on the number in a cell, then I would download the PDF of the sheet. But the name would automatically be the sheet's name. Is there a script I could use so that the name of the pdf file would be one of the data on a specific cell?

I would gladly appreciate the help. This is so I could get this done more quickly since there are so many people.

I tried to follow some code from another thread: [https://stackoverflow.com/questions/56215898/how-to-download-single-sheet-as-pdf-not-export-to-google-drive] And ended up with something like this:

function onOpen() {
  var submenu = [{name:"Save PDF", functionName:"generatePdf"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu('Export', submenu);  
}

function generatePdf() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetId = ss.getActiveSheet().getSheetId();
  var valor = SpreadsheetApp.getActiveSheet().getRange('G2').getValue();  
  var filename = valor;

  // Creat PDF file as a temporary file and create URL for downloading.
  var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
  var blob = UrlFetchApp.fetch(url).getBlob().setName(filename);
  var file = DriveApp.createFile(blob);
  var dlUrl = "https://drive.google.com/uc?export=download&id=" + file.getId();

  // Open a dialog and run Javascript for downloading the file.
  var str = '<script>window.location.href="' + dlUrl + '"</script>';
  var html = HtmlService.createHtmlOutput(str);
  SpreadsheetApp.getUi().showModalDialog(html, "sample");
  file.setTrashed(true);

  // This is used for closing the dialog.
  Utilities.sleep(3000);
  var closeHtml = HtmlService.createHtmlOutput("<script>google.script.host.close()</script>");
  SpreadsheetApp.getUi().showModalDialog(closeHtml, "sample");
}

Didn't work, just maybe one time after so many tries. Then I tried this: https://gist.github.com/primaryobjects/6370689c6f5fd3799ea53f89551eced7

But the PDF that exports have the cell's lines in the background... So, it doesn't look good.

Is there a way for the export to use the pdf download method, so it looks clean, but automatically put the name of the pdf from a cell value?

BamBam
  • 1
  • 1
  • 1
    Here are all of the methods for a spreadsheet: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet – Cooper Jul 19 '21 at 23:04
  • 1
    Here are all of the methods for a sheet: https://developers.google.com/apps-script/reference/spreadsheet/sheet – Cooper Jul 19 '21 at 23:04
  • 1
    Welcome to [so]. Please show what you tried and add add a brief description of your search efforts as is suggested in [ask]. – Rubén Jul 20 '21 at 01:03

0 Answers0