我正在尝试切换到动态库导入 API(来自过时的脚本标签)。我根据Google 电子表格中有关 Apps Script的文档制作了一个示例。结果,我希望在浏览器中获得 Google 地图,但在控制台中收到错误
userCodeAppPanel:2 未捕获的 SyntaxError:输入意外结束 userCodeAppPanel:6 未捕获(在承诺中)TypeError:无法读取未定义的属性(读取‘importLibrary’)在 initMap(userCodeAppPanel:6:39)在 userCodeAppPanel:15:1
以下是 Apps 脚本的代码:
<!doctype html>
<html>
<head>
<title>Simple Map</title>
<script>
let map;
// initMap is now async
async function initMap() {
// Request libraries when needed, not in the script tag.
const { Map } = await google.maps.importLibrary("maps");
// Short namespaces can be used.
map = new Map(document.getElementById("map"), {
center: { lat: 40.72476, lng: -74.04843 },
zoom: 8,
});
}
initMap();
</script>
</head>
<body>
<div id="map"></div>
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src="https://maps.${c}apis.com/maps/api/js?"+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
({key: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "weekly"});</script>
<style>
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
background-color: grey;
}
</style>
</body>
</html>
但在jsfiddle中同样的代码处理没有错误(结果显示为地图)。
我认为 Apps Script 中缺少某些内容。请帮助我了解原因并在动态库导入上运行代码
更新:修复引号部分解决了问题(感谢 Tanaike)。现在错误仍然存在:
未捕获(在承诺中)TypeError:无法读取未定义的属性(读取“importLibrary”)
要重现错误,您可以使用带有模态窗口调用的Google 电子表格。首先,按 F12 在浏览器中显示控制台,然后按脚本按钮并打开地图