【问题标题】:Getting data from a Google Sheet and using it to calculate costs从 Google 表格中获取数据并使用它来计算成本
【发布时间】:2021-02-19 08:45:14
【问题描述】:

我有一个计算器,它可以在用户输入数量时运行顺畅,它会根据输入计算价格。

calculateQuantity(price) {

if (this.quantity <= 75) return 3.20;

if (this.quantity <= 100) return 2.72;

if (this.quantity <= 300) return 2.60;

if (this.quantity <= 500) return 2.58;

if (this.quantity >= 500) return 2.56;

}

calculate() {

subtotal = this.calculateQuantity(subtotal);

subtotal = subtotal * this.quantity;

this.subtotal = subtotal;

现在我需要链接一个每天都会更新的 Google 表格,以确定上述代码中的价格,但我不知所措。我已经浏览了 Google API,并且我已经将工作表显示在页面上,但不知道如何获取特定单元格并将其用作价格。

基本上我认为我想要的是类似的东西

if (this.quantity <= 75) return (ROW 1:COLUMN 1);

如果有人可以为我提供一些启示。我已经搜索了谷歌,我发现的大部分内容只是如何基本显示工作表,而不是如何获取特定数据,它是否在代码中。

也许有更好的解决方案,非技术客户可以轻松输入价格以获取,甚至 Excel 会更好。

完整代码链接:https://jsfiddle.net/y893j5zm/

【问题讨论】:

  • 您可以使用 GET 请求从 Google 表格中读取值。详情请见developers.google.com/sheets/api/samples/reading
  • 关于I've went through the google api and I've gotten the sheet to display on a page but cant figure out how to grab a specific cell and use it a the price.,可以问一下这个细节吗?而且,虽然我看到了你的脚本,但我找不到与此相关的脚本。我为此道歉。那么,您能否提供经过测试的脚本以使用 Google 电子表格?
  • 题外话:为什么函数上有price参数,却从来没用过?

标签: javascript google-sheets google-sheets-api


【解决方案1】:

以下是您需要执行的说明:

  1. 创建一个 Google 表格并将您的“评分”从 A1 放到 A5(3.20、2.72 等等...)。然后,您可以每天更新它。

  2. 创建一个链接到 Google 工作表的 Apps 脚本。在脚本中,您需要创建一个函数来执行以下操作:

    • 具有一个输入参数(数量)的函数。
    • 根据数量的值,返回不同的“等级”(A1到A5)。

创建 Apps 脚本的参考链接:https://developers.google.com/apps-script/guides/sheets

  1. 将应用程序脚本部署为 web 应用程序。然后,您的页面可以使用 HTTP GET 调用此 API 来计算“评分”。

将 Apps 脚本创建为 webapp 的参考链接:https://developers.google.com/apps-script/guides/web

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 2018-08-17
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2019-10-26
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多