簡體   English   中英

如何在 google-apps-script 中包含 Column K != null 條件?

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

我有一個代碼,每當我運行它時,它都會將數據從一張工作表復制粘貼到另一個工作簿。 但是,我只希望從 K 列不為空的原始工作表中復制特定數據。 有人可以幫助推薦我如何將其合並到我的代碼中嗎?

提前致謝!

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);

} 

您可以遍歷數據並將具有非空列 K 的所有值放入另一個數組中:

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

然后將targetData粘貼到目標表中。

在此處復制最終代碼:

function transferList() 
{
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Colombo");
  var sourceData = sourceSheet.getDataRange().getValues();
  sourceData.splice(0,1);  // Remove header
  var rows = sourceSheet.getDataRange();
  var data = rows.getValues();
  var targetData = [];

  for (var i in sourceData) {
  if (sourceData[i][10] !== null && sourceData[i][10] !== '') {
    targetData.push(sourceData[i]);
  }
}
  var targetSS = SpreadsheetApp.openById("TargetSheetLink").getSheetByName("Tester");
  var targetRangeTop = targetSS.getLastRow(); // Get # rows currently in target
     targetSS.getRange(targetRangeTop+1,1,targetData.length,targetData[0].length).setValues(targetData);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM