首页 > 解决方案 > 从 Google 表格中获取数据并使用它来计算成本

问题描述

我有一个计算器,它可以在用户输入数量并根据该输入计算价格的情况下顺利运行。

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/

标签: javascriptgoogle-sheetsgoogle-sheets-api

解决方案


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

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

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

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

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

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

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


推荐阅读