Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How to include Column K != null condition in google-apps-script?

I have a code that copy pastes data from one sheet to another workbook whenever I run it. However, I only want specific data to copy from the original sheet where column K is not null. Could someone help recommend how I can incorporate that in my code?

Thanks in advance!

function transferList() 
{
   var sourceSheet = 
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Colombo");
 var sourceData = sourceSheet.getDataRange().getValues();
 sourceData.splice(0,1);  // Remove header
 var targetSS = . 
SpreadsheetApp.openById("TargetSheetID").getSheetByName("Tester");
  var targetRangeTop = targetSS.getLastRow(); // Get # rows currently in target
 targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);

} 
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You could iterate through your data and put all the values the have non-null column K into another array:

var targetData = [];
for (var i in sourceData) {
  if (sourceData[i][11] !== null && sourceData[i][11] !== '') {
    targetData.push(sourceData[i]);
  }
}

Then you'd paste targetData into your target sheet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...