OfficeScript package

接口

OfficeScript.DownloadFileProperties

要下载的文件。

OfficeScript.EmailAttachment

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

OfficeScript.MailProperties

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

枚举

OfficeScript.EmailContentType

内容的类型。 可能的值为文本或 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

示例

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

    // Download the PDF.
    OfficeScript.downloadFile(pdfFile);

    // 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.Metadata.getScriptName()

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

export function getScriptName(): string;

返回

string

示例

/**
 * This script gets the name of the current script and prints it to the console.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the name of the currently running script.
  const scriptName = OfficeScript.Metadata.getScriptName();
  console.log("The currently running script is: " + scriptName);

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

示例

/**
 * This script saves a copy of the current workbook to the user's OneDrive.
 */
function main(workbook: ExcelScript.Workbook) {    
    OfficeScript.saveCopyAs("CopyOfBook2.xlsx"); 
}

OfficeScript.sendMail(mailProperties)

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

export function sendMail(mailProperties: MailProperties): void;

参数

返回

void

示例

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