javascript - 从 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 会更好。
解决方案
以下是您需要执行的说明:
创建一个 Google 表格并将您的“评分”从 A1 放到 A5(3.20、2.72 等等......)。然后,您可以每天更新它。
创建一个链接到 Google 工作表的 Apps 脚本。在脚本中,您需要创建一个函数来执行以下操作:
- 具有一个输入参数(数量)的函数。
- 根据数量的值,返回不同的“等级”(A1 到 A5)。
创建 Apps 脚本的参考链接:https ://developers.google.com/apps-script/guides/sheets
- 将 Apps 脚本部署为 webapp。然后,您的页面可以使用 HTTP GET 调用此 API 来计算“评分”。
将 Apps 脚本创建为 webapp 的参考链接:https ://developers.google.com/apps-script/guides/web
推荐阅读
- r - 格图 | 图例未填写
- python - 关于 sqlalchemy 中的 unique=True 和 (unique=True, index=True)
- java - 在 Spring 4 + MVC 中不能在 Thymeleaf 上使用片段
- visual-studio-code - 在 VSCode 中使用 Prettier 切换保存时自动格式化
- jenkins - Github url 没有与 jenkins 集成
- java - 购物车中的最后一项
- python - 当句子有引号或引号时如何制作字符串?Python
- python - 登录表单提交失败而没有抛出错误
- asp.net - 有人可以告诉我这里的代码有什么问题吗?
- reactjs - CSS2DRenderer 不在 Three.js 中