Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - Moving filtered values (copy and paste values) using google sheet script while ignoring the column heading / title

I would like to copy and paste VALUES only from source sheet to target sheet using google scripts.

I have filter applied in the column so I am only looking to copy the cells that are present / filtered (not all values).

In the example that I have built, I have:

Source sheet (copy values from): Sheet1 Target sheet (paste values to): Sheet2

Sheet1 screenshot:

https://i.imgur.com/TySwfpb.jpg

Process / Steps:

When I select any particular color in "Fav_color" in column A..let's say "yellow".

Now, Members are filtered accordingly.

On the button click, I want my google script function to run that copies the filtered Member Names and paste it in Sheet2.

Problem: In Sheet 2: I am getting column heading / title as well "Member Name" while I only want member names (not the actual column name).

I do know the problem as in my code, I am copying complete B column but I do not know how to exclude column heading / title.

function transfer() {
 var sss = SpreadsheetApp.getActiveSpreadsheet();
 var ss = sss.getSheetByName('Sheet1'); 
 var range = ss.getRange('A1:A'); 
 var data = range.getValues();

 var tss = SpreadsheetApp.getActiveSpreadsheet();
 var ts = tss.getSheetByName('Sheet2');
 ts.getRange('A:A').clearContent();

  ts.getRange('A:A').clearContent();
  ss.getRange("B:B").copyTo(ts.getRange("A2"), {contentsOnly:true});

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue:

If you use,

ss.getRange("B2:B").copyTo(ts.getRange("A2"), {contentsOnly:true});

it always bring the value from B2 cell even if it is not visible in filtered results

Solution:

You can get the first non-filtered row number using Sheet#isRowHiddenByFilter

Snippet:

function copycolB() {
  var ss = SpreadsheetApp.getActive();
  var sourceSheet = ss.getSheetByName('Sheet1');
  var targetSheet = ss.getSheetByName('Sheet2');
  for(var row = 2; sourceSheet.isRowHiddenByFilter(row); ++row);//get first  visible row from row2
  var sourceRange = sourceSheet.getRange('B' + row + ':B');
  sourceRange.copyTo(
    targetSheet.getRange('A2'),
    {contentsOnly:true});
}

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...