Edit

Share via


OfficeScript package

Interfaces

OfficeScript.EmailAttachment

The attachment to send with the email. A value must be specified for at least one of the to, cc, or bcc parameters. If no recipient is specified, the following error is shown: "The message has no recipient. Please enter a value for at least one of the "to", "cc", or "bcc" parameters."

OfficeScript.FileProperties

The file to download.

OfficeScript.MailProperties

The properties of the email to be sent.

Enums

OfficeScript.EmailContentType

The type of the content. Possible values are text or HTML.

OfficeScript.EmailImportance

The importance value of the email. Corresponds to "high", "normal", and "low" importance values available in the Outlook UI.

Functions

OfficeScript.convertToPdf()

Converts the document to a PDF and returns the text encoding of it. Note: Recent changes made to the workbook in Excel on the web, through Office Scripts or the Excel UI, may not be captured in the PDF.

OfficeScript.downloadFile(fileProperties)

Downloads a specified file to the default download location specified by the local machine.

OfficeScript.Metadata.getScriptName()

Get the name of the currently running script.

OfficeScript.saveCopyAs(filename)

Saves a copy of the current workbook in OneDrive, in the same directory as the original file, with the specified file name.

OfficeScript.sendMail(mailProperties)

Send an email with an Office Script. Use MailProperties to specify the content and recipients of the email.

Function Details

OfficeScript.convertToPdf()

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Converts the document to a PDF and returns the text encoding of it. Note: Recent changes made to the workbook in Excel on the web, through Office Scripts or the Excel UI, may not be captured in the PDF.

export function convertToPdf(): string;

Returns

string

The content of the workbook as a string, in PDF format.

Throws: ConvertToPdfEmptyWorkbook The error thrown if the document is empty.

Throws: ConvertToPdfProtectedWorkbook The error thrown if the document is protected.

Throws: ExternalApiTimeout The error thrown if the API reaches the timeout limit of 30 seconds.

Examples

/**
 * This script saves a worksheet as a PDF and emails that 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.

    // 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]
    })    
}

OfficeScript.downloadFile(fileProperties)

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Downloads a specified file to the default download location specified by the local machine.

export function downloadFile(fileProperties: FileProperties): void;

Parameters

fileProperties
OfficeScript.FileProperties

The file to download.

Throws: DownloadFileNameMissing The error thrown if the name is empty.

Throws: DownloadFileContentMissing The error thrown if the content is empty.

Throws: DownloadFileInvalidExtension The error thrown if the file name extension is not ".txt" or ".pdf".

Throws: ExternalApiTimeout The error thrown if the API reaches the timeout limit of 30 seconds.

Returns

void

OfficeScript.Metadata.getScriptName()

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Get the name of the currently running script.

export function getScriptName(): string;

Returns

string

OfficeScript.saveCopyAs(filename)

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Saves a copy of the current workbook in OneDrive, in the same directory as the original file, with the specified file name.

export function saveCopyAs(filename: string): void;

Parameters

filename

string

The file name of the copied and saved file. The file name must end with ".xlsx".

Throws: InvalidExtensionError The error thrown if the file name doesn't end with ".xlsx".

Throws: SaveCopyAsFileMayAlreadyExistError The error thrown if the file name of the copy already exists.

Throws: SaveCopyAsErrorInvalidCharacters The error thrown if the file name contains invalid characters.

Throws: SaveCopyAsFileNotOnOneDriveError The error thrown if the document is not saved to OneDrive.

Throws: ExternalApiTimeout The error thrown if the API reaches the timeout limit of 30 seconds. Note that the copy may still be created.

Returns

void

OfficeScript.sendMail(mailProperties)

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Send an email with an Office Script. Use MailProperties to specify the content and recipients of the email.

export function sendMail(mailProperties: MailProperties): void;

Parameters

Returns

void