OfficeScript package

接口

OfficeScript.DownloadFileProperties

要下载的文件。

OfficeScript.EmailAttachment

要随电子邮件一起发送的附件。 必须至少为 、ccbcc 参数之to一指定一个值。 如果未指定收件人,则显示以下错误:“邮件没有收件人。 请为至少一个“to”、“cc”或“bcc”参数输入值。

OfficeScript.MailProperties

要发送的电子邮件的属性。

枚举

OfficeScript.EmailContentType

内容的类型。 可能的值为 text 或 HTML。

OfficeScript.EmailImportance

电子邮件的重要性值。 对应于 Outlook UI 中可用的“高”、“正常”和“低”重要性值。

函数

OfficeScript.convertToPdf()

将文档转换为 PDF 并返回其文本编码。

OfficeScript.downloadFile(fileProperties)

将指定的文件下载到本地计算机指定的默认下载位置。

OfficeScript.Metadata.getScriptName()

获取当前正在运行的脚本的名称。

OfficeScript.saveCopyAs(filename)

使用指定的文件名将当前工作簿的副本保存在与原始文件相同的目录中的 OneDrive 中。 必须在其他 API 之前调用此 API。

OfficeScript.sendMail(mailProperties)

使用 Office 脚本发送电子邮件。 使用 MailProperties 指定电子邮件的内容和收件人。

函数详细信息

OfficeScript.convertToPdf()

将文档转换为 PDF 并返回其文本编码。

export function convertToPdf(): string;

返回

string

工作簿的内容作为字符串,采用 PDF 格式。

引发ConvertToPdfEmptyWorkbook 如果文档为空,则引发。

引发ConvertToPdfProtectedWorkbook 如果文档受保护,则引发。

引发ExternalApiTimeout 如果 API 达到 30 秒的超时限制,则引发。

示例

/**
 * 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)

将指定的文件下载到本地计算机指定的默认下载位置。

export function downloadFile(fileProperties: DownloadFileProperties): void;

参数

fileProperties
OfficeScript.DownloadFileProperties

要下载的文件。

引发DownloadFileNameMissing 如果名称为空,则引发。

引发DownloadFileContentMissing 如果内容为空,则引发。

引发DownloadFileInvalidExtension 如果文件扩展名不是“.txt”或“.pdf”,则引发。

引发ExternalApiTimeout 如果 API 达到 30 秒的超时限制,则引发。

返回

void

OfficeScript.Metadata.getScriptName()

获取当前正在运行的脚本的名称。

export function getScriptName(): string;

返回

string

OfficeScript.saveCopyAs(filename)

使用指定的文件名将当前工作簿的副本保存在与原始文件相同的目录中的 OneDrive 中。 必须在其他 API 之前调用此 API。

export function saveCopyAs(filename: string): void;

参数

filename

string

复制并保存的文件的文件名。 文件名必须以“.xlsx”结尾。

引发SaveCopyAsInvalidExtension 如果文件名不以“.xlsx”结尾,则引发。

引发SaveCopyAsMustBeCalledFirst 如果在其他 API 之后调用此方法,则引发。

引发SaveCopyAsFileMayAlreadyExist 如果副本的文件名已存在,则引发。

引发SaveCopyAsInvalidCharacters 如果文件名包含无效字符,则引发。

引发SaveCopyAsFileNotOnOneDrive 如果文档未保存到 OneDrive,则引发。

引发ExternalApiTimeout 如果 API 达到 30 秒的超时限制,则引发。 请注意,仍可能创建副本。

返回

void

OfficeScript.sendMail(mailProperties)

使用 Office 脚本发送电子邮件。 使用 MailProperties 指定电子邮件的内容和收件人。

export function sendMail(mailProperties: MailProperties): void;

参数

返回

void