Edit

Share via


OfficeScript package

Interfaces

OfficeScript.DownloadFileProperties

The file to download.

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.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.

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. This API must be called before other APIs.

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()

Converts the document to a PDF and returns the text encoding of it.

export function convertToPdf(): string;

Returns

string

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

Throws: ConvertToPdfEmptyWorkbook Thrown if the document is empty.

Throws: ConvertToPdfProtectedWorkbook Thrown if the document is protected.

Throws: ExternalApiTimeout 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)

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

export function downloadFile(fileProperties: DownloadFileProperties): void;

Parameters

fileProperties
OfficeScript.DownloadFileProperties

The file to download.

Throws: DownloadFileNameMissing Thrown if the name is empty.

Throws: DownloadFileContentMissing Thrown if the content is empty.

Throws: DownloadFileInvalidExtension Thrown if the file name extension is not ".txt" or ".pdf".

Throws: ExternalApiTimeout Thrown if the API reaches the timeout limit of 30 seconds.

Returns

void

OfficeScript.Metadata.getScriptName()

Get the name of the currently running script.

export function getScriptName(): string;

Returns

string

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. This API must be called before other APIs.

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: SaveCopyAsInvalidExtension Thrown if the file name doesn't end with ".xlsx".

Throws: SaveCopyAsMustBeCalledFirst Thrown if this method is called after other APIs.

Throws: SaveCopyAsFileMayAlreadyExist Thrown if the file name of the copy already exists.

Throws: SaveCopyAsInvalidCharacters Thrown if the file name contains invalid characters.

Throws: SaveCopyAsFileNotOnOneDrive Thrown if the document is not saved to OneDrive.

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

Returns

void

OfficeScript.sendMail(mailProperties)

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