我制作了一张新的虚拟表。我已将预期结果和当前结果包含在当前代码中。我已尽最大努力尝试编写此代码,但最终对我来说效果不佳。基本上,我需要将空白 Trans 及其数量添加到一个 Trans 中。价格和折扣相同。这是我之前的问题的延伸,所以如果您需要一些说明,也请检查一下。如果您需要更多信息或帮助,请告诉我。如果您能提供帮助,非常感谢。非常感谢任何帮助/建议!非常感谢您的努力和帮助!如果还需要什么,请告诉我。
链接是sheet。
注意:请忽略变量/参数“类别”,因为它与本主题/问题无关。
AppsScript 代码:
var RemoveDupV1 = "";
var RemoveDubVV1 = "";
var Time2List = [];
var Trans2List = [];
var RemoveDupV2 = "";
var RemoveDupV3 = "";
var RemoveDupV4 = 0;
var RemoveDupV5 = 0;
var RemoveDupV6 = 0;
function RemoveDup1(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2) {
if(Trans1.localeCompare(Trans2) == 0){
if(Time1.localeCompare(Time2) == 0){
RemoveDup2V1(Trans2, Time2);
PropertiesService.getScriptProperties().setProperty('myArray', Trans2);
return(Trans1);
}else{
return(Trans1)
}
}else{
return(Trans1)
}
}
function RDR1(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2){
RemoveDupV1 = RemoveDup1(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2);
return(RemoveDupV1)
}
function RemoveDup2V1(Trans2,Time2) {
Trans2List.push(Trans2);
Time2List.push(Time2);
}
// Time
function RemoveDup2(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2) {
if(Trans1.localeCompare(Trans2) == 0){
if(Time1.localeCompare(Time2) == 0){
return(Time1);
}else{
return(Time1)
}
}else{
return(Time1)
}
}
function RDR2(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2){
RemoveDupV2 = RemoveDup2(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2);
return(RemoveDupV2)
}
// Category
function RemoveDup3(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2) {
if(Trans1.localeCompare(Trans2) == 0){
if(Time1.localeCompare(Time2) == 0){
return(Category);
}else{
return(Category)
}
}else{
return(Category)
}
}
function RDR3(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2){
RemoveDupV3 = RemoveDup3(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2);
return(RemoveDupV3)
}
// Quantity
function RemoveDup4(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2) {
if(Trans1.localeCompare(Trans2) == 0){
if(Time1.localeCompare(Time2) == 0){
return(Quantity1 + Quantity2);
}else{
return(Quantity1)
}
}else{
return(Quantity1)
}
}
function RDR4(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2){
RemoveDupV4 = RemoveDup4(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2);
return(RemoveDupV4)
}
// Disc
function RemoveDup5(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2) {
if(Trans1.localeCompare(Trans2) == 0){
if(Time1.localeCompare(Time2) == 0){
return(Disc1 + Disc2);
}else{
return(Disc1)
}
}else{
return(Disc1)
}
}
function RDR5(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2){
RemoveDupV5 = RemoveDup5(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2);
return(RemoveDupV5)
}
// Price
function RemoveDup6(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2) {
if(Trans1.localeCompare(Trans2) == 0){
if(Time1.localeCompare(Time2) == 0){
return(Price1 + Price2);
}else{
return(Price1)
}
}else{
return(Price1)
}
}
function RDR6(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2){
RemoveDupV6 = RemoveDup6(Trans1, Trans2, Time1, Time2, Category, Quantity1, Quantity2, Disc1, Disc2, Price1, Price2);
return(RemoveDupV6)
}
function clean1() {
var data = SpreadsheetApp.getActive().getActiveSheet().getRange('A2:B11').getDisplayValues();
var res = data.map((x, i, arr) => {
try {
return (x[0] == arr[i + 1][0] && x[1] == arr[i + 1][1]) ? ['', x[1]] : arr[i + 1];
} catch {
}
}).filter(x => x);
res.unshift(data[0])
SpreadsheetApp.getActive().getActiveSheet().getRange(2, 7, res.length, res[0].length).setValues(res);
}
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Clear Dupes')
.addItem('Clear all Dupes', 'clean1')
.addSeparator()
.addToUi();
}
从您的以下回复来看,
如果你看到虚拟表,你可以在底部看到预期的结果,基本上空白的交易上面有一个主要交易。代码需要通过将空白交易中的所有内容相加来输出正确的数量、折扣金额和价格。
当使用您提供的电子表格时,以下示例脚本怎么样?
请将以下脚本复制并粘贴到 Google Spreadsheet 的脚本编辑器中并保存脚本。
当您在提供的电子表格中使用此脚本时,请将
=SAMPLE(A1:E11)
的自定义函数放入单元格中。这样,脚本就会运行并返回结果值。
const SAMPLE = values =>
[...values.reduce((m, r) => {
const [a, , ...c] = r;
if (m.has(a)) {
const [aa, bb, ...cc] = m.get(a);
m.set(a, [aa, bb, ...cc.map((e, i) => e + c[i])]);
} else {
m.set(a, r);
}
return m
}, new Map()).values()];
当使用您提供的电子表格运行此脚本时,将获得以下结果。在此示例中,我将自定义函数
=SAMPLE(A1:E11)
放入单元格“G14”中。结果可以在红色范围内看到。
如果行数较多,自定义函数可能无法使用。在这种情况下,请使用脚本编辑器运行脚本。到时候,请使用下面的脚本。
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getDataRange();
const values = [...range.getValues().reduce((m, r) => {
const [a, , ...c] = r;
if (m.has(a)) {
const [aa, bb, ...cc] = m.get(a);
m.set(a, [aa, bb, ...cc.map((e, i) => e + c[i])]);
} else {
m.set(a, r);
}
return m
}, new Map()).values()];
range.clearContent().offset(0, 0, values.length, values[0].length).setValues(values);
}