在office脚本中,我尝试从私有Github仓库访问csv文件。为此,我使用以下fetch调用:
let token = "TOKEN"
const headers = {
"Authorization": `token ${token}`
}
let url = "https://raw.githubusercontent.com/ORGANISATION/REPO/main/data/example.csv
const res = await fetch(url, { headers });
字符串
当我检查浏览器开发工具时,我看到以下错误:
Access to fetch at "{github url}" from origin '{officescripts_url}.officescripts.microsoftusercontent.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
型
我的办公室脚本控制台告诉我这一点(我替换了一些网址只是为了确保):
{message: "Failed to fetch", name: "TypeError", stack: "TypeError: Failed to fetch at self.fetch ({url}) at importCSV ({url}) at async main (blob:{url}) at async _____wrapperMain (blob:{url})"}
型
当我把同样的请求放在Incubator中时,我可以毫无问题地获得我的数据。
是否有方法在Office脚本中修复或解决此问题?
1条答案
按热度按时间w6mmgewl1#
您可以从私有和公共GitHub存储库获取文本文件。
获取文件URL
1.在Github上单击存储库中的文件
1.点击
raw
?token=...
URL示例
public
https://raw.githubusercontent.com/wandyezj/data/master/lists/shapes.list.txt
个私有
https://raw.githubusercontent.com/wandyezj/simple-website/master/LICENSE?token=GHSAT0AAAAAACJUEK6DGRAUMSQW3WZA6YLMZKD2B3A
个Office脚本示例
字符串