gpt4 book ai didi

javascript - 仅生成一张电子表格的 PDF

转载 作者:行者123 更新时间:2023-11-28 17:35:40 24 4
gpt4 key购买 nike

我需要一个只用一张电子表格创建 PDF 的脚本。我目前有一个生成 PDF 的脚本,但它处理整个文件。我无法将这些值复制到另一个文件,因为我需要导出的工作表是带有从另一个工作表中提取的数据的图形。你可以帮帮我吗?谢谢。

function myFunction() {
var archivo = SpreadsheetApp.getActive();
var hoja = archivo.getSheetByName("Graficos 2");
var id = archivo.getId();
var archivoId = DriveApp.getFileById(id);
var archivoBlob = archivoId.getBlob();
var carpetaId = archivoId.getParents();
var contenidoPDF = archivo.getAs(MimeType.PDF);
carpetaId.next().createFile(contenidoPDF);
}

最佳答案

这个问题或多或少已经得到解决here

简而言之,您可以修改Url queryString中的导出参数来获取特定的sheet。然后将 blob 转换为 pdf。

这是相同的代码,基本代码是从 here 获得的

function emailSpreadsheetAsPDF() {

// Send the PDF of the spreadsheet to this email address
var email = "Your Email Id";

// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("<Your Sheet name>")

// Subject of email message
var subject = "PDF generated from sheet " + sheet.getName();

// Email Body can be HTML too with your logo image - see ctrlq.org/html-mail
var body = "Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for one-click conversion.";

// Base URL
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

/* Specify PDF export parameters
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/

var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=letter' // paper size legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id

var token = ScriptApp.getOAuthToken();


//make an empty array to hold your fetched blobs
var blobs;


// Convert your specific sheet to blob
var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});

//convert the response to a blob and store in our array
blobs = response.getBlob().setName(sheet.getName() + '.pdf');


// Define the scope
Logger.log("Storage Space used: " + DriveApp.getStorageUsed());

// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments:[blobs]
});
}

编辑:合并特定工作表并将其转换为 pdf。

首先,您必须获取要合并到 pdf 中的所有工作表的工作表对象

var sheet2 = ss.getSheetByName("<Your Sheet name>")
var sheet3 = ss.getSheetByName("<Your Sheet name>")

然后将每个工作表的sheetId传递给由%EE%B8%80分隔的URL查询,就像这样

   url += url_ext + sheet.getSheetId()+ "%EE%B8%80"+sheet2.getSheetId()+ "%EE%B8%80"+sheet3.getSheetId()
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});

关于javascript - 仅生成一张电子表格的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49197358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com