我只想將值(不是公式)從源范圍復制并粘貼到目標,而沒有重復項(某些值已經在目標范圍內)。我嘗試在線學習幾個教程以從復制粘貼部分開始(甚至不是唯一的條件),但不斷收到錯誤“引數(字串,數字,數字,數字)與電子表格應用程式的方法簽名不匹配.Spreadsheet.getRange ...”。不知道為什么,因為我使用的是相同的代碼。還嘗試使用 copyTo 而不是 setValues 但得到相同的錯誤。
function paste() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source = spreadsheet.getDataRange();
var sourceValues = source.getValues();
var rowCount = sourceValues.length;
var columnCount = sourceValues[0].length;
var target = spreadsheet.getRange(1,4, rowCount, columnCount);
target.setValues(sourceValues);
}
這是我想要獲得的示例電子表格。我知道使用公式會更容易,但問題是我在 Source 中的資料是動態的,因為它來自 ImportXML,所以有時提取的資料會消失,這就是為什么我想在獲取它們之前只粘貼它們丟失的值。
uj5u.com熱心網友回復:
使用filter(),像這樣:
function appendA2bToD2e() {
const ss = SpreadsheetApp.getActive();
const source = ss.getRange('Sheet1!A2:B');
const target = ss.getRange('Sheet1!D2:E');
appendUniquesToRange_(source, target);
}
function appendUniquesToRange_(sourceRange, targetRange) {
const dataToAppend = sourceRange.getValues();
const existingData = targetRange.getValues();
const newData = existingData
.concat(dataToAppend)
.filter((row, rowIndex, array) =>
array
.map(tuple => tuple[0])
.indexOf(row[0]) === rowIndex && row[0] !== ''
);
targetRange
.clearContent()
.offset(0, 0, newData.length, newData[0].length)
.setValues(newData);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/389318.html
下一篇:使用公式生成可用資料的行和列
