将动态行范围复制/追加到另一个工作表

时间:2016-01-24 19:43:00

标签: google-apps-script google-sheets

我问过是否有办法循环应用脚本,根据用户输入的订单项数量将标题库信息复制到多行:

解决方案是使用带有中间表“OrderKey”的arrayformulas,最终可以隐藏它。

感谢Aurielle Perlmann(见下文),我已经更新了工作解决方案。

SHEET系统即将完成

请参阅我的示例工作表:https://docs.google.com/spreadsheets/d/151h1XjB98NOBnO0otNaql3ASjK84CccZZ399dX4BMBM/edit#gid=0

function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom Menu')
// creates a menu item "Submit Sales Order"
.addItem('Submit Sales Order', 'menuItem1')
.addToUi();
}


function menuItem1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var salesheet = ss.getSheetByName("salesOrder");  

var source = ss.getSheetByName(" OrderKey"); 
// sets the 'OrderKey' Sheet as source
var target = ss.getSheetByName("Orders");
// sets 'Orders' sheet as the target for copying data to.
var sourceData = source.getSheetValues(2,1,source.getLastRow(),16);
// sets range to gather source 'OrderKey' data by finding last row, and Line 2 to Column 16
target.getRange(target.getLastRow()+1, 1, sourceData.length,16).setValues(sourceData);
// finds last row of target 'Orders' and writes to +1 row past last row up to column 16 using setValues of sourceData

// Following simply clears Sales Order Sheet so new data can be entered
salesheet.getRange('B4:B5').clearContent();
salesheet.getRange('B8').clearContent();
salesheet.getRange('G6:G8').clearContent();
salesheet.getRange('F10:G10').clearContent();
salesheet.getRange('A13:C76').clearContent();
salesheet.getRange('J13:J76').clearContent();

// Following gets seed number from cell I1, and increases value by +1 so next Sales Order ID is incremented by 1
var cell = salesheet.getRange("I1");
var cellValue = cell.getValue();
cell.setValue(cellValue + 1);

  var lastID = salesheet.getRange("F1");
  var nextID = salesheet.getRange("G1");

  var lastIDValue = lastID.getValue();
  nextID.setValue(lastIDValue + 1);
}

更新。

1 个答案:

答案 0 :(得分:2)

我继续为您添加了工作表中的公式,此公式只需要添加到标题后的第一行,其余的将自动填写

使用的两个公式是:

while (cin >> a >> b >> c) {
    // Computations...
    cout << output << endl;
}

DECLARE @Employee TABLE(id INT,name VARCHAR(100));
INSERT INTO @Employee VALUES
 (1,'Smith')
,(2,'Patel')
,(6,'Bennett')
,(7,'Booch');

DECLARE @Car TABLE(regNo VARCHAR(100),distanceSinceService INT,kmPerLitre FLOAT);
INSERT INTO @Car VALUES
 ('FF06DDS',122,10)
,('EE07SSA',110,8)
,('FG07BAK',165,7)
,('HH08BBW',0,12);

DECLARE @TravelCost TABLE(jouneyID INT,carRegNo VARCHAR(100),employeeID INT,occured DATE,distanceInKm FLOAT);
INSERT INTO @TravelCost VALUES
 (4,'FF06DDS',1,{d'2007-07-11'},90)
,(6,'FG07BAK',2,{d'2007-07-21'},110)
,(11,'EE07SSA',6,{d'2007-07-21'},110)
,(12,'FF06DDS',1,{d'2007-09-05'},32)
,(21,'FG07BAK',7,{d'2007-09-05'},55);

SELECT e.name AS Employee
      ,SUM(tc.distanceInKm) AS SumKm
      ,SUM(tc.distanceInKm/c.kmPerLitre) AS UsedGasoline 
FROM @TravelCost AS tc
INNER JOIN @Car AS c ON tc.carRegNo=c.regNo
INNER JOIN @Employee AS e ON tc.employeeID=e.id
GROUP BY e.name
HAVING SUM(tc.distanceInKm)>100;

单元格引用会根据您尝试导入的字段而更改。

在完成该工作表之后 - 我添加了一个脚本,然后我将其附加到一个名为“submit”的按钮,该按钮获取最后一行并将该值附加到可用于存档的单独工作表上:

Employee    SumKm   UsedGasoline
Bennett     110     13,75
Patel       110     15,71
Smith       122     12,2