Typescript Office脚本上的界面错误(Web上的Excel)

Typescript Office脚本上的界面错误(Web上的Excel),typescript,fetch,excel-365,office-scripts,Typescript,Fetch,Excel 365,Office Scripts,我正在尝试从web上的Excel调用外部API() 我正在调用的Web API返回一个jason数据结构,例如此处“检索客户”会话下的“响应”: 响应数据结构: { "Customer": { "@url": "https://api.fortnox.se/3/customers/102", "Active": true, "Address1"

我正在尝试从web上的Excel调用外部API()

我正在调用的Web API返回一个jason数据结构,例如此处“检索客户”会话下的“响应”:

响应数据结构:

{
    "Customer": {
        "@url": "https://api.fortnox.se/3/customers/102",
        "Active": true,
        "Address1": "Halltorpsgatan",
        "Address2": null,
        "City": "KLIPPAN",
        "Comments": null,
        "CostCenter": null,
        "Country": "Sverige",
        "CountryCode": "SE",
        "Currency": "SEK",
        "CustomerNumber": "102",
        "DefaultDeliveryTypes": {
            "Invoice": "PRINT",
            "Offer": "PRINT",
            "Order": "PRINT"
        },
        "DefaultTemplates": {
            "CashInvoice": "DEFAULTTEMPLATE",
            "Invoice": "DEFAULTTEMPLATE",
            "Offer": "DEFAULTTEMPLATE",
            "Order": "DEFAULTTEMPLATE"
        },
        "DeliveryAddress1": null,
        "DeliveryAddress2": null,
        "DeliveryCity": null,
        "DeliveryCountry": null,
        "DeliveryCountryCode": null,
        "DeliveryFax": null,
        "DeliveryName": null,
        "DeliveryPhone1": null,
        "DeliveryPhone2": null,
        "DeliveryZipCode": null,
        "Email": "a.s@example.com",
        "EmailInvoice": "a.s@example.com",
        "EmailInvoiceBCC": "",
        "EmailInvoiceCC": "",
        "EmailOffer": "a.s@example.com",
        "EmailOfferBCC": "",
        "EmailOfferCC": "",
        "EmailOrder": "a.s@example.com",
        "EmailOrderBCC": "",
        "EmailOrderCC": "",
        "Fax": null,
        "GLN": null,
        "GLNDelivery": null,
        "InvoiceAdministrationFee": null,
        "InvoiceDiscount": null,
        "InvoiceFreight": null,
        "InvoiceRemark": "",
        "Name": "Anders Svensson",
        "OrganisationNumber": "",
        "OurReference": "",
        "Phone1": "0435-9249236",
        "Phone2": null,
        "PriceList": "A",
        "Project": "",
        "SalesAccount": null,
        "ShowPriceVATIncluded": false,
        "TermsOfDelivery": "",
        "TermsOfPayment": "",
        "Type": "PRIVATE",
        "VATNumber": "",
        "VATType": "SEVAT",
        "VisitingAddress": null,
        "VisitingCity": null,
        "VisitingCountry": null,
        "VisitingCountryCode": null,
        "VisitingZipCode": null,
        "WWW": "",
        "WayOfDelivery": "",
        "YourReference": "",
        "ZipCode": "264 32"
    }
}
受Stackoverflow帖子的启发:我编写了以下代码:

interface aCustomer {
  Customer: CustomerClass;
}

interface CustomerClass {
  Address1: string;
  Country: string;
}

async function main(workbook: ExcelScript.Workbook): Promise<void> {
  let response = await fetch("https://api.fortnox.se/3/customers/2", {
    method: "GET",
    mode: "no-cors",
    headers: {
      "Access-Token": "XXXX",
      "Client-Secret": "XXXX",
      "Content-Type": "application/json",
      "Accept": "application/json"
    }
  });
  let customer2: aCustomer = await response.json();
  console.log(customer2);
}
interface aCustomer{
客户:CustomerClass;
}
接口客户类{
地址1:字符串;
国家:字符串;
}
异步函数main(工作簿:ExcelScript.workbook):承诺{
let response=等待获取(“https://api.fortnox.se/3/customers/2", {
方法:“获取”,
模式:“无cors”,
标题:{
“访问令牌”:“XXXX”,
“客户机密”:“XXXX”,
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
}
});
让customer2:aCustomer=wait response.json();
控制台日志(customer2);
}
但当我运行时,它不断抛出错误:“第21行:输入意外结束”

第21行:let customer2:Customer=wait response.json()


我想我定义了一个错误的接口,或者知道问题出在哪里了吗?谢谢

您尝试使用的API似乎最有可能由桌面客户端应用程序或服务器应用程序使用,但不是直接由浏览器中的客户端JavaScript使用(CORS可能会成为一个问题)

一种可能的解决方法是使用“获取代理”。简而言之,它是一个简单的web服务器,将原始请求传递给目标API,并将响应发送回客户端JavaScript。这可能会起作用,因为此获取代理服务器和目标API(服务器到服务器通信)之间不会出现CORS问题,同时此获取代理服务器配置为允许客户端JavaScript的CORS

您可以构建并托管自己的Fetch代理服务器,也可以尝试一些现有的代理服务器。但是,如果您关心数据隐私或性能,您可能希望构建和托管自己的数据。这里特别提到了一种现有的流行的Fetch代理服务器实现(“CORS-Anywhere”)


对于CORS问题和获取代理的想法也有一些很好的解释。

这是否回答了您的问题?类型定义还不是问题。我想你的数据提取有问题。看看我提到的链接,hanks@Nishant。你的意思是我不应该在fetch()方法中使用'no cors'模式吗?虽然当我尝试不使用“mode:‘no cors’”时,它在第11行“let response=wait fetch”(“…有什么办法可以解决它吗?谢谢!