問題:最新庫存更新時,專案的詳細資訊不能一起移動。
下圖顯示了初始輸入:

下圖顯示了庫存變化時的問題:

期望的輸出:

重復項的名稱列公式:
=QUERY(ArrayFormula(FLATTEN(SPLIT(REPT($AL11:$AL13&"@",$AM11:$AM13),"@"))),"where Col1 is not null")
uj5u.com熱心網友回復:
建議
使用應用程式腳本
不確定是否可以使用公式,但這是您可以使用的腳本。當然,這仍然需要一些調整以與您的電子表格保持一致。
另外,我對腳本所做的是,如果您將其更改為較低的庫存,它會洗掉姓氏和詳細資訊,因為它更有意義,而不是保持在同一個位置。
嘗試:
function onEdit(e) {
const ss = e.source;
const range = e.range;
if(range.getColumn() == 2 && range.getRow() > 1) {
const sheet = ss.getActiveSheet();
const lastRow = sheet.getLastRow();
const dataRange = sheet.getRange(2, 1, lastRow - 1, 2);
const dataValues = removeEmptyRows(dataRange.getValues());
const outputRange = dataRange.offset(0, 2);
const outputData = removeEmptyRows(outputRange.getValues());
let output = [];
dataValues.filter(x => x[1] && x[1] > 0).forEach(row => {
const [fruit, count] = row;
let currentFruit = outputData.filter(x => x[0] == fruit);
let currentCount = currentFruit.length, rows;
if(currentCount < count) {
rows = count - currentCount;
currentFruit.push(...Array(rows).fill([fruit, '']));
}
else if(count < currentCount) {
rows = currentCount - count;
currentFruit = currentFruit.slice(0, -1 * rows);
}
output.push(...currentFruit);
});
outputRange.clearContent();
sheet.getRange(2, 3, output.length, output[0].length).setValues(output);
}
}
function removeEmptyRows(array) {
return array.filter(x => x.filter(String).length > 0)
}
結果:

讓我知道這是否有效或您有其他問題。
編輯 - 修改后的代碼
function onEdit(e) {
const ss = e.source;
const range = e.range;
if(range.getColumn() == 2 && range.getRow() > 1) {
// Needed values to modify
// Input: Data that holds how many items are in each category
const inputRowStart = 2; // Input starts at:
const inputColStart = 1; // ROW 2, COL 1 (A2)
// Output: Data where the output is written
const outputRowStart = 2; // Output starts at:
const outputColStart = 3; // ROW 2, COL 3 (C2)
const outputCols = 3; // 3 COLS (Fruits, Details, Date)
const sheet = ss.getActiveSheet();
const lastRow = sheet.getLastRow();
const inputRange = sheet.getRange(inputRowStart, inputColStart, lastRow - 1, 2);
const inputValues = removeEmptyRows(inputRange.getValues());
const outputRange = sheet.getRange(outputRowStart, outputColStart, lastRow - 1, outputCols);
const outputData = removeEmptyRows(outputRange.getValues());
let output = [];
inputValues.filter(row => row[1] > 0).forEach(row => {
const [fruit, count] = row;
let currentFruit = outputData.filter(row => row[0] == fruit);
let currentCount = currentFruit.length, rows;
if(currentCount < count) {
rows = count - currentCount;
currentFruit.push(...Array(rows).fill([fruit, ...Array(outputCols - 1).fill('')]));
}
else if(count < currentCount) {
currentFruit = currentFruit.slice(0, count);
}
output.push(...currentFruit);
});
outputRange.clearContent();
sheet.getRange(outputRowStart, outputColStart, output.length, output[0].length).setValues(output);
}
}
function removeEmptyRows(array) {
return array.filter(row => row.filter(String).length > 0)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/493964.html
