【问题标题】:Method to return data from different ranges using the same function使用相同函数从不同范围返回数据的方法
【发布时间】:2019-04-27 02:04:54
【问题描述】:

我正在寻找一种方法来使用脚本中的相同函数来返回多个不同的数据范围以输出到 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

有没有办法在多个产品上使用相同的脚本,还是必须为每个产品制作一个副本?

【问题讨论】:

  • 一般来说,我说可能。但是,如果您可以分享一些电子表格并更准确地解释您的需求,我们可以更好地确定问题。
  • 我用一些图片和更多信息更新了我的帖子。希望它更有意义

标签: google-apps-script google-sheets


【解决方案1】:

使用对话框从库存表中订购零件

显然,这在现实生活中会复杂得多,但这只是帮助您入门的示例。您可能希望确保活动页面是一个订单并且可能具有今天的日期,然后您可能希望选择一个数量并从库存中减去该数量。所以事情会很快变得更复杂。

如果这是我为客户做的工作,我实际上会使用模板化的 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]);
}

我的库存表:

我的订单:

点击菜单中的显示订单对话框,出现对话框

您要订购的零件旁边的点击按钮,零件出现在订单上

Templated Html

Google Apps Script Documentation

【讨论】:

  • 我将这些方法应用到我自己的工作表上,效果很好。谢谢!不过我还有一个问题,是否可以添加脚本,以便您可以从订单对话框中调整数量值。第二张表与其说是库存,不如说是可以订购的产品清单,所以数量就是需要订购多少。我调整了脚本以将该数字添加到订单中,但如果可以从对话框中更改数字,那就太好了。再次感谢!
  • 是的。但是要按照我向您展示的方式进行操作并不容易,因为我必须将脚本编写为文本。最好将其作为模板化的 html 解决方案来执行,以便脚本更易于阅读和编写。我可以为你整理一个例子。我会发布它可能是明天。可能我会问另一个问题,以免页面过于复杂。
  • 我会考虑用模板化的 html 来做这件事,并试图弄清楚。感谢您的帮助
  • 谢谢,我现在正在做模板化的 html,我认为它可以工作
  • 如果您想参考,欢迎复制。我还将库存数量更新添加回对话框,如果低于零,也会将背景变为红色。
猜你喜欢
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多