google-apps-script - 使用相同函数从不同范围返回数据的方法
问题描述
我正在寻找一种方法来使用脚本中的相同函数来返回多个不同的数据范围以输出到 Google 表格中的另一个表格。
这是我第一次尝试使用 Google 脚本进行任何自动化,所以请多多包涵。该项目被设计为一个从库存表下订单的系统。库存表包含订购过程中使用的产品信息和部件号。我创建了一个带有按钮触发器的脚本,它将数据从一张表复制到订单表,以后可以打印出来。这允许其他工作人员拥有下订单所需的所有信息。问题是我只能使用每个脚本将一个产品复制到订单表中,还有其他方法吗?
产品库存(复制自)
订购单(复制到)
第一个图像是从中复制数据的位置,它将被添加到第二个显示的工作表中。我希望能够使用另一个运行与第一个脚本相同的脚本的按钮将第二行信息复制到订单表。(数量行由用户填写,也会被复制到订单中)
这是按钮脚本:
//Copies Part Info
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheets()[1])// Sets the active sheet to the complete inventory
var range = sheet.getRange('A2:D2')// This is the one that I am able to copy
var values = range.getValues()// Stores the product info
return values;
//Pastes Info to Order
var activeSheet = SpreadsheetApp.setActiveSheet(ss.getSheets()[0])// Sets the order sheet as active
var lastRow = activeSheet.getLastRow()//The next few lines find the next empty row on the sheet to paste the next data into
var nextRow = parseInt(lastRow)+1
var reqRange = activeSheet.getRange("A1:D100")
var inputCell = reqRange.getCell(nextRow,1)
var addData = inputCell.setValue(values)//Pastes the part info into the availabe cell
有没有办法在多个产品上使用相同的脚本,还是必须为每个产品制作一个副本?
解决方案
使用对话框从库存表中订购零件
显然,这在现实生活中会复杂得多,但这只是一个让您入门的示例。您可能希望确保活动页面是订单并且可能具有今天的日期,然后您可能希望选择一个数量并从库存中减去该数量。所以事情会很快变得更复杂。
如果这是我为客户做的工作,我实际上会使用模板化的 html 方法,您可以在下面的参考资料中阅读。
function onOpen() {//Drop Down Menu You will need to run this from the editor to get it to create a menu. Or close and then open the spreadsheet. If you have other menus then make sure you put this in another project.
SpreadsheetApp.getUi().createMenu('Order Parts')
.addItem('Show Order Dialog', 'getInventory')
.addToUi();
}
function getInventory() {//show order dialog for selecting parts
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Inventory');
var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
var pA=rg.getValues();
var html="<style>td,th{border:1px solid black;}</style><table>";
html+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td></tr>','Brand','Product Description','Item','Quantity')
for(var i=0;i<pA.length;i++) {
html+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><input type="button" value="Order" onClick="moveToOrder(%s);" /></td></tr>',pA[i][0],pA[i][2],pA[i][2],pA[i][3],i+2)
}
html+='</table><br /><input type="button" value="Close" onClick="google.script.host.close();" />';
html+='<script>function moveToOrder(row){google.script.run.moveToOrder(row);};console.log("MyCode");</script>';
var userInterface=HtmlService.createHtmlOutput(html).setWidth(600).setHeight(300).setTitle('Inventory');
SpreadsheetApp.getUi().showModelessDialog(userInterface,'Inventory');
}
function moveToOrder(row) { //moves selected part to active page so make sure your on the right order form
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Inventory');
var rg=sh.getRange(row,1,1,3);
var pA=rg.getValues();
var osh=ss.getActiveSheet();
osh.appendRow(pA[0]);
}
我的库存表:
我的订单:
单击菜单中的显示订单对话框并出现对话框
您希望订购的零件旁边的单击按钮,零件出现在订单上
推荐阅读
- python - 需要从 JSON 响应中提取信息?
- python - 我无法使用 CV 将我的 RGB 颜色更改为 python 中的灰度图像
- node.js - IN()表达式中的nodejs bigquery参数化查询
- c# - 如何使用 C# 获取和设置 DLL 文件详细信息
- swift5 - 你能解释一下以下 Swift Projected Value 的输出吗?
- linux - Bash:将字符串翻转到分隔符的另一侧
- c++ - 在视觉工作室处理多个项目
- reactjs - 如何使用反应、笑话和酶对自定义表格单元格渲染器功能进行单元测试?
- arrays - 使用 Python 中的高级索引进行 nd 数组收缩的简单方法
- kotlin - Kotlin:动态选择伴生对象函数