如何使用Office脚本在Office 365中发送电子邮件

发布于 2025-02-04 01:25:42 字数 1725 浏览 2 评论 0原文

我目前正在创建一个办公脚本,以从Excel Online发送电子邮件(托管在OneDriveForBusiness中)。电子邮件的组成部分是从Excel单元格值中提取的。已经做了一项研究,看来Office脚本具有类似于JavaScript或Typescript的语法。我也不确定所使用的语言是javaScript还是Typescript,请为此澄清。

总而言之,

  1. 我需要一个有关如何在excel Online中从Office脚本发送电子邮件的代码示例(Office 365),
  2. 我需要知道Office脚本中使用了哪种语言,是JavaScript或Typescript。

以下是示例代码Office脚本所编写的,并且在我运行时会失败。

function main(workbook: ExcelScript.Workbook) {
    // Your code here

  // select a sheet
  let selected_sheet = workbook.getActiveWorksheet()

  // get subject
  let subject = selected_sheet.getRange("C2")
  let subject_ = subject.getValue()

  // get body
  let body = selected_sheet.getRange("E2")
  let body_ = body.getValue()

  // to email
  let receivembx = selected_sheet.getRange("B2")
  let receivembx_ = receivembx.getValue()

  // from email
  let sendmbx = "emailhere"
  let pwd_ = "passwordhere"

  Email.send({
    Host: "smtp.office365.com",
    Username: sendmbx,
    Password: pwd_,
    To: receivembx_,
    From: sendmbx,
    Subject: subject_,
    Body: body_,

  }).then(
    console.log("mail sent successfully")
  );

  console.log(sendmbx)
}

运行该代码后,我得到

以下是Excel在线和Office脚本的剪辑。

I'm currently creating an office script to send email from excel online(hosted in onedriveforbusiness). The components of the email am extracting from excel cell values. Have done a research and it looks to me that office script has syntax similar to javascript or typescript. I'm also not sure if the language used is javascript or typescript, kindly clarify for me on that.

In summary

  1. I need a code sample on how to send email from office script in excel online(office 365)
  2. I need to know what language is being used in office script, is it javascript or typescript.

Below is the sample code office script have written and its failing when i run it.

function main(workbook: ExcelScript.Workbook) {
    // Your code here

  // select a sheet
  let selected_sheet = workbook.getActiveWorksheet()

  // get subject
  let subject = selected_sheet.getRange("C2")
  let subject_ = subject.getValue()

  // get body
  let body = selected_sheet.getRange("E2")
  let body_ = body.getValue()

  // to email
  let receivembx = selected_sheet.getRange("B2")
  let receivembx_ = receivembx.getValue()

  // from email
  let sendmbx = "emailhere"
  let pwd_ = "passwordhere"

  Email.send({
    Host: "smtp.office365.com",
    Username: sendmbx,
    Password: pwd_,
    To: receivembx_,
    From: sendmbx,
    Subject: subject_,
    Body: body_,

  }).then(
    console.log("mail sent successfully")
  );

  console.log(sendmbx)
}

Upon running that code, i get
enter image description here
enter image description here

Below is the snip for the excel online and office script.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2025-02-11 01:25:42

问题1

所使用的代码编辑器具有更多有限的功能,因此您不允许直接发送电子邮件。使用VBA,这有点直观,因为VBA有能力使用PC(也许太多)。您最好尝试使用PowerApps。

由于您要求提供一个示例,因此从Microsoft考虑一下

请注意,您可以从Excel中的表中捕获字段,因此您每条消息都会获得动态电子邮件地址等。

问题2

您使用的是打字稿,但非常相似。差异描述为

Office脚本是用JavaScript的超集编写的。 ,您在JavaScript中编写的任何脚本代码都可以正常工作。

Question 1

The code editor you're using has more limited capabilities so you aren't permitted to directly send emails. With VBA this is somewhat intuitive as VBA has the power to do a lot with your PC (perhaps too much). You would be better off to try to use powerapps.

Since you asked for an example, consider this one from Microsoft.

Note that you can capture fields from an table in excel so that's how you would get a dynamic email address per message, etc.

Question 2

You are using TypeScript, but very similar. The difference is described as:

"Office Scripts are written in TypeScript, which is a superset of JavaScript. The Action Recorder generates code in TypeScript and the Office Scripts documentation uses TypeScript. Since TypeScript is a superset of JavaScript, any scripting code that you write in JavaScript will work just fine."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文