メニューによるエクスポート機能です。この機能を実行すると、ルートドライブに保存されます。
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
// Add a menu item based on properties (doesn't work in AuthMode.NONE).
var items = [
{name: 'Export', functionName: 'exportToPDF'},
];
ss.addMenu('Export', items);
}
function exportToPDF() {
var sheetName = "Sheet1";
var pdfName = "My Created PDF "+Date();
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf"))
//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
//save to pdf
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = DriveApp.createFile(theBlob);
//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
SpreadsheetApp.getUi().alert('New PDF file created in the root of your Google Drive')
}