如何合并具有相同标识的两行?
How do I combine two rows with a the same identity?
我正在尝试合并具有相同标识的 2 行。我一直在寻找解决方案,但不知何故找不到可行的解决方案。我正在尝试为我的股票制作一个跟踪器,但如果我添加相同的资产,我希望它能合并这些信息。我制作了一个表格,可以将交易添加到投资组合中。因此,如果存在重复资产,新的重复资产将始终位于最后一行。
我既不是编程专家,也不是 google sheets,但这是伪代码:
- 检查第2列(资产id)是否重复,如果是:
- 将最后一行第一列的值复制到现有行(这是购买日期的一列);
- 资产的id在第2列,所以可以保持不变;
- 金额在第 3 列,它应该将最后一行的金额添加到现有行的第 3 列。
这是一个例子sheet:https://docs.google.com/spreadsheets/d/1AEdljHtXUOnRJ1kxbziqKAjYo5EqGZjjnWOx1mbeTI0/edit#gid=0
我尝试了几件事,但都卡住了。我编写了一个代码来遍历数据,找到重复项并将其添加到列表中。但是之后就不知道怎么办了
你可能会嘲笑我的代码,但从某种意义上说,这就像在原地打转。
function readData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Portfolio");
var rangeArray = formSS.getRange("B2:B" + formSS.getLastRow()).getValues(); //makes an array of values from column B, but each item is in array in itself.
rangeArray = [].concat.apply([],rangeArray);//takes the elements outside their own array;
var sortedRangeArray = rangeArray.sort();//sort the items to a new array
duplicates=[];//make a list of duplicates to identify
for (var i =0; i < 1;sortedRangeArray.length, i++)//iterate through sortedArray
if(sortedRangeArray[i+1] === sortedRangeArray[i]){
duplicates.push(sortedRangeArray[i]);//if a duplicate is found, push it to the duplicates list
}
var str = duplicates[0];//identify the duplicate, there is only one anyway.
for (var k = 0; k < sortedRangeArray.length; k++) {
var row = sortedRangeArray[k];
if(row[SEARCH_COL_IDX] == str) {
var index = rangeArray.findIndex(str);//I thought it might help defining the position
}}}```
假设这些是你的 headers [ amount, in, new Amount]
function run(){
const ACTIVE = SpreadsheetApp.getActive()
const allRows = ACTIVE.getDataRange().getValues()
const lastRow = allRows.pop()
// Id is in column B
const recordId = lastRow[1]
// Amount is in column A
const amount = lastRow[0]
// Remove row 1, I assume there are only headers
allRows.shift()
// Look through every row in Column B for a match
allRows.forEach( (row, index) => {
if( row[1] == recordId ){
// Create a new amount
const newAmount = amount + parseInt(row[0])
ACTIVE.getRange(index+2, 3).setValue(newAmount)
}
})
}
可能是这样的:
function main() {
var table = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var ids = table.map(x => x[1]); // get array of IDs (column B)
var row = ids.indexOf(ids.pop()); // find ID from last row in the array
if (row == -1) return; // if not found do nothing
var value = table[row][2]; // get value of 3rd cell
table[row] = table.pop(); // move the last row in current row
table[row][2] += value; // add the value to 3rd cell
table.push(Array(table[0].length)); // add empty row to the table
SpreadsheetApp.getActiveSheet().getDataRange().setValues(table); // put the table back on the sheet
}
更新
由于您的 table 包含公式,因此需要稍作其他实施:
function main() {
var sheet = SpreadsheetApp.getActiveSheet();
var table = sheet.getDataRange().getValues();
var ids = table.map(x => x[1]); // get array of IDs (column B)
var row = ids.indexOf(ids.pop()); // find ID from last row in the array
if (row == -1) return; // if not found do nothing
var last_row = table.pop();
var date = last_row[0];
var id = last_row[1];
var value = last_row[2] + table[row][2];
sheet.getRange(row+1,1,1,3).setValues([[date,id,value]]);
sheet.getRange(sheet.getLastRow(),1,1,8).clearContent();
}
我正在尝试合并具有相同标识的 2 行。我一直在寻找解决方案,但不知何故找不到可行的解决方案。我正在尝试为我的股票制作一个跟踪器,但如果我添加相同的资产,我希望它能合并这些信息。我制作了一个表格,可以将交易添加到投资组合中。因此,如果存在重复资产,新的重复资产将始终位于最后一行。
我既不是编程专家,也不是 google sheets,但这是伪代码:
- 检查第2列(资产id)是否重复,如果是:
- 将最后一行第一列的值复制到现有行(这是购买日期的一列);
- 资产的id在第2列,所以可以保持不变;
- 金额在第 3 列,它应该将最后一行的金额添加到现有行的第 3 列。
这是一个例子sheet:https://docs.google.com/spreadsheets/d/1AEdljHtXUOnRJ1kxbziqKAjYo5EqGZjjnWOx1mbeTI0/edit#gid=0
我尝试了几件事,但都卡住了。我编写了一个代码来遍历数据,找到重复项并将其添加到列表中。但是之后就不知道怎么办了
你可能会嘲笑我的代码,但从某种意义上说,这就像在原地打转。
function readData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Portfolio");
var rangeArray = formSS.getRange("B2:B" + formSS.getLastRow()).getValues(); //makes an array of values from column B, but each item is in array in itself.
rangeArray = [].concat.apply([],rangeArray);//takes the elements outside their own array;
var sortedRangeArray = rangeArray.sort();//sort the items to a new array
duplicates=[];//make a list of duplicates to identify
for (var i =0; i < 1;sortedRangeArray.length, i++)//iterate through sortedArray
if(sortedRangeArray[i+1] === sortedRangeArray[i]){
duplicates.push(sortedRangeArray[i]);//if a duplicate is found, push it to the duplicates list
}
var str = duplicates[0];//identify the duplicate, there is only one anyway.
for (var k = 0; k < sortedRangeArray.length; k++) {
var row = sortedRangeArray[k];
if(row[SEARCH_COL_IDX] == str) {
var index = rangeArray.findIndex(str);//I thought it might help defining the position
}}}```
假设这些是你的 headers [ amount, in, new Amount]
function run(){
const ACTIVE = SpreadsheetApp.getActive()
const allRows = ACTIVE.getDataRange().getValues()
const lastRow = allRows.pop()
// Id is in column B
const recordId = lastRow[1]
// Amount is in column A
const amount = lastRow[0]
// Remove row 1, I assume there are only headers
allRows.shift()
// Look through every row in Column B for a match
allRows.forEach( (row, index) => {
if( row[1] == recordId ){
// Create a new amount
const newAmount = amount + parseInt(row[0])
ACTIVE.getRange(index+2, 3).setValue(newAmount)
}
})
}
可能是这样的:
function main() {
var table = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var ids = table.map(x => x[1]); // get array of IDs (column B)
var row = ids.indexOf(ids.pop()); // find ID from last row in the array
if (row == -1) return; // if not found do nothing
var value = table[row][2]; // get value of 3rd cell
table[row] = table.pop(); // move the last row in current row
table[row][2] += value; // add the value to 3rd cell
table.push(Array(table[0].length)); // add empty row to the table
SpreadsheetApp.getActiveSheet().getDataRange().setValues(table); // put the table back on the sheet
}
更新
由于您的 table 包含公式,因此需要稍作其他实施:
function main() {
var sheet = SpreadsheetApp.getActiveSheet();
var table = sheet.getDataRange().getValues();
var ids = table.map(x => x[1]); // get array of IDs (column B)
var row = ids.indexOf(ids.pop()); // find ID from last row in the array
if (row == -1) return; // if not found do nothing
var last_row = table.pop();
var date = last_row[0];
var id = last_row[1];
var value = last_row[2] + table[row][2];
sheet.getRange(row+1,1,1,3).setValues([[date,id,value]]);
sheet.getRange(sheet.getLastRow(),1,1,8).clearContent();
}