我是新人,无法理解。这是场景
1)我有一个表,其中我有3列,其中一列有一个类别值。
2)我正在编写一个脚本,我从表中选择范围并在该特定列中查找该值。
3)如果该行有我要查找的值,我想复制表格的整行并将其粘贴到新的电子表格上。
function myFunction(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(“Sheet1”);
var range = sheet.getRange('B1:B35');
var destination = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(“Sheet3”);
var verduras ='verduras';
var rule = SpreadsheetApp.newDataValidation()。requireTextEqualTo('Category')。copy();
range.setDataValidation(规则); range.copyValuesToRange(destination,1,2,1,2);
}
解决方案:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TableWithInformation");
var columnToSearch = 2; // B column
var lastrow = sheet.getLastRow();
Logger.log(sheet.getLastRow());
var range =sheet.getRange(1,columnToSearch,sheet.getLastRow(),columnToSearch);
var destination = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Report");
var values = range.getValues();
//Destination
var numberOfColumns = 3;
var numberOfRows = 1 ;
var startColumn = 1;
var startRow = 2;
// equivalent to destination.getRange('A1:C3');
var count = 0
var destRow = destination.getLastRow()+1
for(var i=0;i<values.length;i++){
Logger.log("i is now: " +i);
Logger.log("Current row value: " +values[i][0])
if(values[i][0] == 'Category'){
Logger.log("*** Value found in cloumn: " +values[i][0] +"**** \n Count: " +i);
count++;
var rangeToCopy = sheet.getRange((i+1), 1,numberOfRows,numberOfColumns);
var destinationRange = destination.getRange(destRow, startColumn,numberOfRows,numberOfColumns);
destRow++;
Logger.log("Next Destination row: " + destRow);
rangeToCopy.copyTo(destinationRange);
}
}
}
答案 0 :(得分:0)
我真的无法通过数据验证规则获得您想要实现的目标。
我会使用循环并搜索您要查找的值。如果在范围中找到该值,则复制该范围的一个或多个值。
下面是一个示例,您需要修改它以复制整行,如果这是您想要的。这只会复制B列中的值。
编辑:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var columnToSearch = 2; // B column
Logger.log(sheet.getLastRow());
var range =sheet.getRange(1,columnToSearch,sheet.getLastRow(),columnToSearch);
var destination = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
var values = range.getValues();
//Destination
var numberOfColumns = 3;
var numberOfRows = 1 ;
var startColumn = 1;
var startRow = 1;
// equivalent to destination.getRange('A1:C3');
var destinationRange= destination.getRange(startRow, startColumn,numberOfRows,numberOfColumns);
var count = 0;
for(var i=0;i<values.length;i++){
Logger.log("i is now: " +i);
Logger.log("Current row value: " +values[i][0])
if(values[i][0] == 'Category'){
Logger.log("*** Value found in cloumn: " +values[i][0] +"**** \n Count: " +i);
count++;
var rangeToCopy = sheet.getRange(i, 1,numberOfRows,numberOfColumns);
rangeToCopy.copyTo(destinationRange);
}
}
}
我认为如果你学习一些基本的控制结构和javascript会更容易。网上有很多好资源,例如:
http://www.codecademy.com/en/tracks/javascript
另请查看谷歌参考页面,了解具体的谷歌应用程序功能。
https://developers.google.com/apps-script/reference/spreadsheet/range
答案 1 :(得分:0)
尝试这样的事情:
function myFunction() {
var values = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getDataRange().getValues();
for(var i = 0, len = values.length; i < len; i++){
if(values[i][1] == 'Category'){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3").appendRow(values[i]);
}
}
}