在Excel或Excel外接程序之外使用Office.js或Office脚本



我正在尝试获取Excel 中活动单元格的值

我可以通过编写一个简单的Excel加载项来获取值。

我也能够使用Office Scripts 做同样的事情

Office脚本:

function main(workbook: ExcelScript.Workbook) {
// Get the current active cell in the workbook.
let cell = workbook.getActiveCell();
// Log that cell's value.
console.log(`The current cell's value is ${cell.getValue()}`);
}

Excel 中的Office脚本

外接程序使用了类似的代码:使用的股票代码(示例代码(做了相同的工作

主页.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<title></title>
<script src="../Scripts/jquery-3.5.0.js" type="text/javascript"></script>
<script src="../Scripts/MessageBanner.js" type="text/javascript"></script>
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
<!-- To enable offline debugging using a local reference to Office.js, use: -->
<!-- <script src="../Scripts/Office/MicrosoftAjax.js" type="text/javascript"></script>  -->
<!-- <script src="../Scripts/Office/1/office.js" type="text/javascript"></script>  -->
<script src="Home.js" type="text/javascript"></script>
<link href="Home.css" rel="stylesheet" type="text/css" />
<link href="../Content/Button.css" rel="stylesheet" type="text/css" />
<link href="../Content/MessageBanner.css" rel="stylesheet" type="text/css" />

<!-- For the Office UI Fabric Core, go to https://aka.ms/office-ui-fabric to learn more. -->
<link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-core/9.6.0/css/fabric.min.css">
<!-- To enable the offline use of Office UI Fabric Core, use: -->
<!-- link rel="stylesheet" href="../Content/fabric.min.css" -->

<!-- Office UI Fabric JS and it's components are no longer actively supported. Please see https://aka.ms/PnP-OfficeFabricReact for recommended Patterns and Practices -->
<!-- <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.components.min.css"> -->
</head>
<body class="ms-Fabric" dir="ltr">
<div id="content-main">
<div class="padding">
<br />
<div class="ms-font-xxl ms-fontColor-themeDarkAlt ms-fontWeight-semilight">Welcome</div>
<p class="ms-font-m-plus">This example will read the current document selection.</p>
<button class="Button Button--primary" id="get-data-from-selection">
<span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span>
<span class="Button-label">Get data from selection</span>
<span class="Button-description">Gets the selected data and displays it.</span>
</button>
<br /><br /><br />
<div class="ms-font-m"><a target="_blank" class="ms-Link ms-Link--hero" href="https://go.microsoft.com/fwlink/?LinkId=276813">Find more samples online...</a></div>
</div>
</div>
<!-- Component used for displaying notifications -->
<div class="MessageBanner" style="position:absolute;bottom: 0;">
<div class="MessageBanner-content">
<div class="MessageBanner-text">
<div class="MessageBanner-clipper">
<div class="ms-font-m-plus ms-fontWeight-semibold" id="notificationHeader"></div>
<div class="ms-font-m ms-fontWeight-semilight" id="notificationBody"></div>
</div>
</div>
<button class="MessageBanner-expand" style="display:none"><i class="ms-Icon ms-Icon--chevronsDown"></i> </button>
<div class="MessageBanner-action"></div>
</div>
<button class="MessageBanner-close"> <i class="ms-Icon ms-Icon--ChromeClose"></i> </button>
</div>
</body>
</html>

Home.js

(function () {
"use strict";
var messageBanner;
// The initialize function must be run each time a new page is loaded
Office.initialize = function (reason) {
console.log("Inside Initialization")
$(document).ready(function () {
var element = document.querySelector('.MessageBanner');
messageBanner = new components.MessageBanner(element);
messageBanner.hideBanner();
$('#get-data-from-selection').click(getDataFromSelection);
});
console.log("done Initialization")
};
// Reads data from current document selection and displays a notification
function getDataFromSelection() {
if (Office.context.document.getSelectedDataAsync) {
Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
function (result) {
if (result.status === Office.AsyncResultStatus.Succeeded) {
showNotification('The selected text is:', '"' + result.value + '"');
} else {
showNotification('Error:', result.error.message);
}
}
);
} else {
app.showNotification('Error:', 'Reading selection data is not supported by this host application.');
}
}

// Helper function for displaying notifications
function showNotification(header, content) {
$("#notificationHeader").text(header);
$("#notificationBody").text(content);
messageBanner.showBanner();
messageBanner.toggleExpansion();
}
})();

现在,我正在尝试从Excel外部运行此脚本,类似于VBScript,在那里我可以在Excel外部独立运行脚本,并获取活动工作簿、活动单元格和相应的值。

我试着从excel外部运行JS代码我得到这个错误:

Warning: Office.js is loaded outside of Office client
office.js:46 The add-in is not hosted in plain browser top window.
Ewa.js:1 SendShuttingDownRequest: https://excel.officeapps.live.com/x/RemoteUls.ashx?officeserverversion=16.0.14206.35904&waccluster=GSH7; requestApi: f

Office.js在Office客户端外部加载

我在浏览器上的Sharepoint中使用Excel我正在将JS代码注入网页(Excel Sharepoint页面(,JS代码调用Office.initializegetDataFromSelection()(如上所述(也加载了"https://appsforoffice.microsoft.com/lib/1/hosted/office.js",并试图获取Excel的活动单元格值。

但一旦我调用Initialize,Excel Sharepoint页面就会变为空白,这可能是关闭的原因(可能是这个SendShuttingDownRequest的原因(。

在我当前的用例中,我将不得不使用外部脚本从Excel Sharepoint页面获取数据,而且我将无法使用Power Automate来运行脚本。

  1. 我在使用JS API的过程中是否做错了什么
  2. 有没有一种类似于VB脚本的方法可以使用JS API从Excel中获取数据

不能在外接程序的上下文之外使用Office.js。它需要从Excel主机获取信息才能进行初始化。

最新更新