excel Office脚本不允许使用acces token从私有github repo获取数据

czfnxgou  于 7个月前  发布在  Git
关注(0)|答案(1)|浏览(72)

在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脚本中修复或解决此问题?

w6mmgewl

w6mmgewl1#

您可以从私有和公共GitHub存储库获取文本文件。

获取文件URL

1.在Github上单击存储库中的文件
1.点击raw

  • 复制地址栏中的整个URL
  • 对于私有存储库,您需要确保在URL中包含令牌:?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脚本示例

/**
 * Script to show how to fetch public text file data from GitHub and insert it into a worksheet.
 */
async function main(workbook: ExcelScript.Workbook) {

    // Read a text file on github
    // This file contains a list of shapes
    const request = await fetch("https://raw.githubusercontent.com/wandyezj/data/master/lists/shapes.list.txt")
    const text = await request.text();
    console.log(text);

    // Insert into new worksheet
    const rows = text.split("\n").map(x => [x])
    workbook.addWorksheet().getRange(`A1:A${rows.length}`).setValues(rows);
}

字符串

相关问题