I want a code or a Formula that can copy a row to another spreadsheet based on the text on the column B, but i just want to copy the Column A,B,C and D...so for example, if column B name is Stator, i want to copy the row to the spreadsheet Stator, if the name is Alternator, i want to copy the row to Alternator Spreadsheet.
I already tried this one:
function yourfunction() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName("Stator");
var sh2=ss.getSheetByName("Prototype Number");
var rg1=sh1.getRange(1,2,sh1.getLastRow(),sh1.getLastColumn()-1);//starting at column2
var data=rg1.getValues();
for(var i=1;i<data.length;i++) {
if(data[i][6]=="Estator") {
sh1.getRange(i+1,2,1,sh1.getLastColumn()-1).copyTo(sh2.getRange(sh2.getLastRow()+1,1,1,sh1.getLastColumn()-1));
}
}
}
But didn't work.
I also tried a macro record, but didn't work because I couldn't find a way to paste the row to a new row every time.
Assuming you have data in columns A,B,C,D
in a main sheet Main
. Your goal is to copy the rows ( AD
) to the corresponding sheet its name matches the value in column B
.
You need to iterate over all sheets, and filter only the data (rows) for which column B
matches that sheet name. You can use forEach to iterate over the sheets and filter to filter based on the sheet name - column B
.
Feel free to adjust the name of the sheets or also the ranges but do respect the logic of the script, otherwise it won't work properly.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const main_sheet = 'Main';
const msh = ss.getSheetByName(main_sheet);
const sheets = ss.getSheets();
const data = msh.getRange('A2:D'+msh.getLastRow()).getValues();
sheets.forEach(sh=>{
if(sh.getName()!=main_sheet){
let temp_ar = data.filter(r=>r[1]==sh.getName());
if (temp_ar.length>0){
sh.getRange(sh.getLastRow()+1,1,temp_ar.length,temp_ar[0].length).setValues(temp_ar);
}}});
}
Structure of my example file:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.