Freigeben über


Speichern eines Arbeitsblatts und Senden einer E-Mail als PDF

Verwenden Sie Office-Skripts, um ein Arbeitsblatt als PDF zu speichern und es per E-Mail an sich selbst oder Ihr Team zu senden.

Lösung

  1. Erstellen Sie eine neue Excel-Datei auf Ihrem OneDrive.
  2. Fügen Sie Ihrer Arbeitsmappe Daten hinzu.
  3. Erstellen Sie das Skript aus diesem Beispiel.
  4. Ersetzen Sie name@email.com in diesem Beispiel durch die gewünschte Empfänger-E-Mail-Adresse.
  5. Passen Sie die subject Werte und content an.
  6. Führen Sie das Skript aus.

Beispielcode: Als PDF speichern und per E-Mail senden

/**
 * This script saves a worksheet as a PDF, downloads that PDF to your computer, and emails the PDF to a recipient.
 */
function main(workbook: ExcelScript.Workbook) {    
    // Create the PDF.
    const pdfObject = OfficeScript.convertToPdf();
    const pdfFile = { name: "report.pdf", content: pdfObject }; // Enter your desired PDF name here.
    
    // Download the PDF.
    OfficeScript.downloadFile(pdfFile); // Not required. Remove this line if you don't want to download the PDF.
    
    // Email the PDF.
    OfficeScript.sendMail({
        to: "name@email.com", // Enter your recipient email address here.
        subject: "[Demo] Monthly Sales Report", // This is the subject of your email.
        content: "Here's the Monthly Sales Report", // This is the content within your email.
        attachments: [pdfFile]
    })    
}

Tipp

Verwenden Sie die Eigenschaften der MailProperties-Schnittstelle , um Ihrer E-Mail weitere Details hinzuzufügen, z cc. B. die Werte , bccund importance .

Problembehandlung

Fehler: Geschütztes Dokument

Die Vertraulichkeitsbezeichnung für Ihre Arbeitsmappe verhindert, dass das Skript eine E-Mail sendet. Um diesen Fehler zu beheben, ändern Sie die Vertraulichkeitsbezeichnung Ihrer Arbeitsmappe in Allgemein, Öffentlich oder Nicht-Geschäft. Laden Sie die Arbeitsmappe neu, und führen Sie das Skript dann erneut aus.