【发布时间】:2021-08-28 15:43:15
【问题描述】:
我正在尝试在 Excel 中获取活动单元格的值
我可以通过编写一个简单的 Excel 插件来获得价值。
我也可以使用 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()}`);
}
插件使用了类似的代码: 使用相同工作的股票代码(示例代码)
主页.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 loaded outside of Office Client
这里我在浏览器的 Sharepoint 中使用 Excel
我将 JS 代码注入网页(Excel Sharepoint 页面),
JS代码调用Office.initialize和getDataFromSelection()(上面提到的)也加载了"https://appsforoffice.microsoft.com/lib/1/hosted/office.js"并试图获取Excel的活动单元格值。
但一旦我调用 Initialize,Excel Sharepoint 页面将因关机而变为空白(可能是此SendShuttingDownRequest 的原因)。
在我当前的用例中,我必须使用外部脚本从 Excel Sharepoint 页面获取数据,而且我将无法使用 Power Automate 运行脚本。
- 我在使用 JS API 的方式上做错了吗?
- 有没有类似于 VB Script 的方式使用 JS API 从 Excel 中获取数据?
【问题讨论】:
标签: ms-office office-js office-addins office-js-helpers