我读了Google Script: Conditionally copy rows from one spreadsheet to another,无法使其对我有用。
我需要一个脚本,该脚本将允许我执行以下引用的文本。请随时编辑my spreadsheet以制作完整的工作脚本。我将公开提供电子表格,任何人都可以复制该脚本供自己使用。我真的对脚本编写一无所知,所以我需要全部阐明。抱歉,我真是个菜鸟,但这可以帮助我学习。
https://docs.google.com/spreadsheet/ccc?key=0AoJdwy8V1ldHdFA5M1M1Wlp1NHZhcmxJOUZKVEU4X3c&usp=sharing
If Column B on sheet "All_Mileage" says "John" then I want that row to be copy and pasted into Sheet "John" starting on row 3 and following.
If Column B on sheet "All_Mileage" says "Adam" then I want that row to be copy and pasted into Sheet "Adam" starting on row 3 and following.
If Column B on sheet "All_Mileage" says "Mike" then I want that row to be copy and pasted into Sheet "Mike" starting on row 3 and following.
我在这里看到了其他脚本,但无法使它们正常工作。就像我说的那样,在编写代码时,我比幼树更环保。
万分感谢!
最佳答案
C.Lang是对的,这不是一个获取现成脚本的地方...但是由于这个问题非常普遍并且经常被问到,所以我花了几分钟的时间来编写和测试...所以它是:
var ss=SpreadsheetApp.getActiveSpreadsheet();// some global variables
var master = ss.getSheetByName('All_Mileage');
var colWidth = master.getMaxColumns();
function copyRowsOnCondition() {
var data = master.getDataRange().getValues();
for(n=2;n<data.length;++n){
if(data[n][1].length<16){ // if not pre-filled with your text
Logger.log(data[n][1])
var dest = ss.getSheetByName(data[n][1].toString().replace(/ /g,''));//remove any spaces that could be included in the name so the name = sheetName for sure
var destRange = dest.getRange(dest.getLastRow()+1,1);// the place to write
master.getRange(n+1,1,1,colWidth).copyTo(destRange);the copy itself value & format
}
}// loop
}
编辑:,因为我在MasterSheet中使用名称值来查找目标表,所以我认为使用相同的规则创建目标表不存在来处理目标表不存在的情况可能会很有用。
name = sheetName
...另一个问题是无法知道已经复制了哪些行...因此我制作了一个可以处理所有内容的版本,仅复制手动选择的行(即使仅在单个列中)并更改背景颜色表示这些行已被处理。我还添加了一个最小舒适的菜单;-)
(如何在一个寒冷的星期日下午忙碌;-)
var ss=SpreadsheetApp.getActiveSpreadsheet();
var master = ss.getSheetByName('All_Mileage');
var colWidth = master.getLastColumn();// last used col in masterSheet
var sheets = ss.getSheets();// number of sheets
function onOpen() {
var menuEntries = [ {name: "Copy selected Rows to sheets", functionName: "copyRowsOnConditionV2"},
];
ss.addMenu("Copy functions",menuEntries);// custom menu
}
function copyRowsOnConditionV2() {
var sheetNames = [];// array of existing sheet names
var sheets = ss.getSheets();// number of sheets
for(s=0;s<sheets.length;++s){sheetNames.push(sheets[s].getName())};
ss.getActiveSelection().setBackground('#ffffbb');
var selectedfirstRow = ss.getActiveSelection().getRowIndex();
var selectedHeigth = ss.getActiveSelection().getHeight()
var selectedFullRange = master.getRange(selectedfirstRow,1,selectedHeigth,colWidth);
var data = selectedFullRange.getValues();
for(n=0;n<data.length;++n){
if(data[n][1].length<16){
if(sheetNames.toString().match(data[n][1].toString().replace(/ /g,''))!=data[n][1].toString().replace(/ /g,'')){// if no sheet exist with this name
var newSheet = ss.insertSheet(data[n][1].toString().replace(/ /g,''),ss.getSheets().length);// then create it
master.getRange(1,1,2,colWidth).copyTo(newSheet.getRange(1,1));// and copy the headers on 2 first rows, then continue as usual
newSheet.getRange(1,1).setValue('Gas Mileage Log - '+data[n][1].toString().replace(/ /g,''));// set name in header
SpreadsheetApp.flush();
var sheets = ss.getSheets();// number of sheets
var sheetNames = [];// update array of existing sheet names
for(s=1;s<sheets.length;++s){sheetNames.push(sheets[s].getName())};
Logger.log(sheetNames)
};
var dest = ss.getSheetByName(data[n][1].toString().replace(/ /g,''));//find the destination sheet
Logger.log(data[n][1].toString().replace(/ /g,''))
var destRange = dest.getRange(dest.getLastRow()+1,1);// define range
master.getRange(selectedfirstRow+n,1,1,colWidth).copyTo(destRange);// and make copy below last row
}
}
}
下图:
关于google-apps-script - Google脚本: Conditionally copy rows from one sheet to another in the same spreadsheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665698/