导入不同版本中的函数的正确方法是什么.js
脚本写入commands.js
Office Excel 功能区加载项的脚本?
这是我的commands.html
file:
<!--
"commands.html"
HTML file importing the necessary scripts, especially commands.js where
button presses are mapped.
-->
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js"></script>
<script src="commands.js" type="module"></script>
这是我的commands.js
file
/*
"commands.js"
All top-level functions that are mapped to button presses on the ribbon
are and should be in this file. Function names are the same as the IDs of
the buttons they are mapped to (with underscored instead of dashes).
*/
import * as tools from "./scripts/native/tools.js"
Office.initialize = function() {
/*
Initialize Office function. Has to be called here.
*/
}
console.log(tools.char_to_underscore("test32"))
这是我想导入的函数tools.js
:
/*
"tools.js"
A collection of functions that help with certain problems.
*/
export function char_to_underscore(string) {
/*
Replaces a set of characters in a string with underscores.
*/
var replacement_list = [/ /g, ".", "\\", "%", "/", "\n", "-", "(", ")", "|"];
for (var replace in replacement_list) {
string = string.replace(replacement_list[replace], "_");
}
return string;
}
这是我的项目目录:
截屏 https://i.stack.imgur.com/sZyCK.png
VSCode 似乎可以完美地找到这些文件,但是当我启动加载项时,出现以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
logger — index.js:551 (https://localhost:3000/scripts/native/tools.js)
我几乎尝试了所有我能想到的方法,但似乎没有任何方法可以解决这个问题。我在这里错过了什么吗?对于大型功能区项目来说,将所有源代码都放在一个文件中是不可持续的。