簡體   English   中英

從 Google 表格中的一系列單元格生成 Email PDF

[英]Generate and Email PDF From a Range of Cells in Google Sheets

這是我第一次發布問題,而且我是一個完全的新手,所以如果我沒有包含您可能需要的所有信息,我會提前道歉。

我有一個 Google 表格,其中包含幾個手動鍵入表格的數據選項卡。 例如:工作表中的數據輸入表

在每張表中,有許多表格,其中輸入了來自特定區域內的每個“公司”的數據。

What I'm trying to do is adjust a script that will go to the sheet specified, grab a range of cells for a specific table, convert it to a PDF and email it using the email address listed in another tab of the same sheet. 我在網上搜索並找到了以下腳本:

 function emailSpreadsheetAsPDF() { DocumentApp.getActiveDocument(); DriveApp.getFiles(); // This is the link to my spreadsheet with the Form responses and the Invoice Template sheets // Add the link to your spreadsheet here // or you can just replace the text in the link between "d/" and "/edit" // In my case is the text: 17I8-QDce0Nug7amrZeYTB3IYbGCGxvUj-XMt8uUUyvI const ss = SpreadsheetApp.openByUrl("SHEETURLGOESHERE/edit"); // We are going to get the email address from the cell "B7" from the "Invoice" sheet // Change the reference of the cell or the name of the sheet if it is different const value = ss.getSheetByName("emails").getRange("A2").getValue(); const email = value.toString(); // Subject of the email message const subject = ss.getSheetByName("emails").getRange("B2").getValue(); // Email Text. You can add HTML code here - see ctrlq.org/html-mail const body = "Sent via Generate Invoice from Google Form and print/email it"; // Again, the URL to your spreadsheet but now with "/export" at the end // Change it to the link of your spreadsheet, but leave the "/export" const url = 'SHEETURLGOESHERE/export?'; const exportOptions = 'exportFormat=pdf&format=pdf' + // export as pdf '&size=letter' + // paper size letter / You can use A4 or legal '&landscape=true' + // orientation portal, use false for landscape '&fitw=true' + // fit to page width false, to get the 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=879200050' // the sheet's Id. Change it to your sheet ID. '&if=false' + '&ic=false' + '&r1=51' + '&c1=0' + '&r2=102' + '&c2=20'; // You can find the sheet ID in the link bar. // Select the sheet that you want to print and check the link, // the gid number of the sheet is on the end of your link. var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}}; // Generate the PDF file var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob(); // Send the PDF file as an attachement GmailApp.sendEmail(email, subject, body, { htmlBody: body, attachments: [{ fileName: ss.getSheetByName("emails").getRange("B2").getValue() + ".pdf", content: response.getBytes(), mimeType: "application/pdf" }] }); // Save the PDF to Drive. The name of the PDF is going to be the name of the Company (cell B5) const nameFile = ss.getSheetByName("01_Allegany_R3").getRange("A52").getValue().toString() +".pdf" DriveApp.createFile(response.setName(nameFile)); }

雖然上面的代碼確實創建了 PDF 和 email 它,但它並沒有從我在這里輸入的范圍內生成 PDF:

 const exportOptions = 'exportFormat=pdf&format=pdf' + // export as pdf '&size=letter' + // paper size letter / You can use A4 or legal '&landscape=true' + // orientation portal, use false for landscape '&fitw=true' + // fit to page width false, to get the 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=879200050' // the sheet's Id. Change it to your sheet ID. '&if=false' + '&ic=false' + '&r1=51' + '&c1=0' + '&r2=102' + '&c2=20';

如果有人能夠為我提供一些幫助,我將不勝感激,但我是新手,不熟悉編寫腳本,所以請耐心等待,如果可能的話:)

謝謝

您需要將gid參數放在導出選項的末尾

  const exportOptions =
    'exportFormat=pdf&format=pdf' + // export as pdf
    '&size=letter' + // paper size letter / You can use A4 or legal
    '&landscape=true' + // orientation portal, use false for landscape
    '&fitw=true' + // fit to page width false, to get the 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
    '&if=false' +
    '&ic=false' +
    '&r1=51' +
    '&c1=0' +
    '&r2=102' +
    '&c2=20'+
    '&gid=879200050'; // the sheet's Id. Change it to your sheet ID.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM