我有這個代碼,它從一列復制資料并將其傳輸到表格底部的另一個作業表。這作業正常,但問題是它復制了整個列,包括空白列。我只想限制要復制到僅包含資料的單元格的單元格。這是代碼:
function addTitles() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
let sourceSheet = spreadSheet.getSheetByName('Data Entry');
let sourceRange = sourceSheet.getDataRange();
let sourceValues = sourceRange.getValues();
sourceValues = sourceValues.slice(1).map(row => row.slice(10,10 1));
let sheetToAdd = spreadSheet.getSheetByName('Cost of Goods');
let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;
let lastRow = sheetToAdd.getLastRow() 1;
let targetSheet = spreadSheet.getSheetByName('Cost of Goods');
let targetRange = targetSheet.getRange(lastRow,1,rowCount,columnCount);
targetRange.setValues(sourceValues);
let fillDown = sheetToAdd.getRange(lastRow, 2, lastRow-1, columnCount);
sheetToAdd.getRange("B4:CV4").copyTo(fillDown);
}
這是樣本表:https : //docs.google.com/spreadsheets/d/1AuzIZqIPbnqqZ0VirAOwY--7ISU22tAnc3ojsVwZRn4/edit?usp=sharing
uj5u.com熱心網友回復:
問題:
如果我理解正確,您想從中過濾掉sourceValues以空字串作為其值的行(您只檢索一列,因此每行只有一個值),使用filter.
解決方案:
如果是這種情況,您可以替換它:
sourceValues = sourceValues.slice(1).map(row => row.slice(10,10 1));
有了這個:
sourceValues = sourceValues.slice(1).map(row => row.slice(10,10 1))
.filter(row => String(row[0]));
參考:
- 篩選
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338327.html
上一篇:如何正確瞄準彈出視窗?
下一篇:動態加載時手風琴不起作用
