基于两列值的差异 - Google 电子表格
Differentiation based on two columns values - Google Spreadsheet
我有以下挑战。
你能做的最好的事情就是看照片,这样你就能更好地理解它。我想在 Google 电子表格中动态显示预订约会之间的区别。
存在以下约会: =
新销售和赢回。
- New Sales = 是新的约会,如果之前有none,那么就没有“lead_id”或者已经有“lead_id”,约会必须超过过去 30 天。
- 赢回 = 如果相同的“lead_id”在30天内再次被预订。
所以我想如果“lead_id”存在,那么最后一个日期从“lead_id”->“oppt_booked_on”减去当前日期“oppt_booked_on” ".
我试了很多,vlookp,match function等等,就是写不出动态公式。始终使用最后一次预订也很重要,如示例(照片)所示。
我希望有一个人可以帮助我。非常感谢。
不确定这是否完全符合您的要求,但公式如下:
=datedif(index(sort(filter(F:F,A:A=A2)),match(F2,sort(filter(F:F,A:A=A2)),0)-1),F2,"d")
在 'Days between_booked on' 列(您的 J 列)中 return 行日期与同一 lead_id 前一个实例的日期之间的天数(列A).
此后,公式如下:
=if(and(countif(A:A,A2)>1,isbetween(J2,1,30)),"Winback","New Sales")
在 'Outcome'(您的第 I 列)中 return 如果当前行的 lead_id 实例超过 1 个,并且当前行和上一个相同的实例 lead_id 小于或等于 30 天。
不确定是否有意义,但这是脚本:
function main() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getActiveSheet();
const range = sh.getDataRange();
const [header, ...rows] = range.getValues();
// get indexes of the columns
const col = {};
col.id = header.indexOf('lead_id')
col.outcome = header.indexOf('Outcome');
col.date = header.indexOf('oppt_booked_on');
col.days = header.indexOf('Days Between booked_on');
// create the object 'data' from the rows
const data = {};
for (let row in rows) {
let id = rows[row][col.id];
try {
data[id].dates.push(rows[row][col.date]);
}
catch(e) {
data[id] = {
outcome: 'New Sales',
days: '',
dates: [rows[row][col.date]],
}
}
}
// change the rows
for (let row in rows) {
let id = rows[row][col.id];
let obj = data[id];
rows[row][col.outcome] = obj.outcome;
rows[row][col.days] = obj.days;
if (obj.dates.length > 1) {
obj.days = days_between(obj.dates.shift(), obj.dates[0]);
obj.outcome = (obj.days < 30) ? 'Winback' : 'New Sales';
}
}
// set the columns 'Outcome' and 'Days...' on the sheet
const outcome = rows.map(x => [x[col.outcome]]);
sh.getRange(2, col.outcome+1, outcome.length, 1).setValues(outcome);
const days = rows.map(x => [x[col.days]]);
sh.getRange(2, col.days+1, days.length, 1).setValues(days);
}
// function returns number of days between two dates
function days_between(date1, date2) {
let msec = Math.abs(date1.getTime() - date2.getTime());
let days = Math.floor(msec/(1000*60*60*24));
return days;
}
随时为您的电子表格修改它。
潜在客户分析
我想要可以复制和粘贴的数据来测试此代码:
function leadAnalysis() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const sr = 2;//data start row
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 6).getValues();
let lo = { lA: [] };
vs.forEach((r, i) => {
if (!lo.hasOwnProperty(r[0])) {
lo[r[0]] = [];
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': 'New Sales', days_between_booked_on: 0 });
sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
lo.lA.push(r[0]);
} else {
let d = DiffInDays1(new Date(lo[r[0]][lo[r[0]].length - 1].oppt_booked_on), new Date(r[5]))
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': (d > 30) ? 'New Sales' : 'WinBack', days_between_booked_on: d });
sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
}
})
}
function DiffInDays1(Day1, Day2) {
if (Day1 && Day2 && (Object.prototype.toString.call(Day1) === '[object Date]') && (Object.prototype.toString.call(Day2) === '[object Date]')) {
var day = 86400000;
var t1 = new Date(Day1).valueOf();
var t2 = new Date(Day2).valueOf();
var d = Math.abs(t2 - t1);
var days = Math.floor(d / day);
//Logger.log(days);
return days;
} else {
throw 'Invalid Inputs';
}
}
通过自己输入得到如下数据如下:
lead_id
oppt_booked_on
Outcome
Days
id1
1/1/2021
New Sales
0
id2
1/2/2021
New Sales
0
id3
1/3/2021
New Sales
0
id4
1/4/2021
New Sales
0
id5
1/5/2021
New Sales
0
id6
1/6/2021
New Sales
0
id1
3/1/2021
New Sales
59
id2
3/2/2021
New Sales
59
id3
3/3/2021
New Sales
59
id4
3/4/2021
New Sales
59
id5
3/5/2021
New Sales
59
id6
3/6/2021
New Sales
59
id1
3/18/2021
WinBack
16
id2
3/19/2021
WinBack
16
id3
3/20/2021
WinBack
16
id4
3/21/2021
WinBack
16
id5
3/22/2021
WinBack
16
id6
3/23/2021
WinBack
16
这个版本会 运行 快得多:
function leadAnalysis() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const sr = 2;//data start row
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 6).getValues();
let lo = { lA: [] };
let oA = [];
vs.forEach((r, i) => {
if (!lo.hasOwnProperty(r[0])) {
lo[r[0]] = [];
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': 'New Sales', days_between_booked_on: 0 });
//sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
oA.push([lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on])
lo.lA.push(r[0]);
} else {
let d = DiffInDays1(new Date(lo[r[0]][lo[r[0]].length - 1].oppt_booked_on), new Date(r[5]))
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': (d > 30) ? 'New Sales' : 'WinBack', days_between_booked_on: d });
//sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
oA.push([lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]);
}
})
sh.getRange(sr,9,oA.length,oA[0].length).setValues(oA)
}
我有以下挑战。
你能做的最好的事情就是看照片,这样你就能更好地理解它。我想在 Google 电子表格中动态显示预订约会之间的区别。
存在以下约会: = 新销售和赢回。
- New Sales = 是新的约会,如果之前有none,那么就没有“lead_id”或者已经有“lead_id”,约会必须超过过去 30 天。
- 赢回 = 如果相同的“lead_id”在30天内再次被预订。
所以我想如果“lead_id”存在,那么最后一个日期从“lead_id”->“oppt_booked_on”减去当前日期“oppt_booked_on” ".
我试了很多,vlookp,match function等等,就是写不出动态公式。始终使用最后一次预订也很重要,如示例(照片)所示。 我希望有一个人可以帮助我。非常感谢。
不确定这是否完全符合您的要求,但公式如下:
=datedif(index(sort(filter(F:F,A:A=A2)),match(F2,sort(filter(F:F,A:A=A2)),0)-1),F2,"d")
在 'Days between_booked on' 列(您的 J 列)中 return 行日期与同一 lead_id 前一个实例的日期之间的天数(列A).
此后,公式如下:
=if(and(countif(A:A,A2)>1,isbetween(J2,1,30)),"Winback","New Sales")
在 'Outcome'(您的第 I 列)中 return 如果当前行的 lead_id 实例超过 1 个,并且当前行和上一个相同的实例 lead_id 小于或等于 30 天。
不确定是否有意义,但这是脚本:
function main() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getActiveSheet();
const range = sh.getDataRange();
const [header, ...rows] = range.getValues();
// get indexes of the columns
const col = {};
col.id = header.indexOf('lead_id')
col.outcome = header.indexOf('Outcome');
col.date = header.indexOf('oppt_booked_on');
col.days = header.indexOf('Days Between booked_on');
// create the object 'data' from the rows
const data = {};
for (let row in rows) {
let id = rows[row][col.id];
try {
data[id].dates.push(rows[row][col.date]);
}
catch(e) {
data[id] = {
outcome: 'New Sales',
days: '',
dates: [rows[row][col.date]],
}
}
}
// change the rows
for (let row in rows) {
let id = rows[row][col.id];
let obj = data[id];
rows[row][col.outcome] = obj.outcome;
rows[row][col.days] = obj.days;
if (obj.dates.length > 1) {
obj.days = days_between(obj.dates.shift(), obj.dates[0]);
obj.outcome = (obj.days < 30) ? 'Winback' : 'New Sales';
}
}
// set the columns 'Outcome' and 'Days...' on the sheet
const outcome = rows.map(x => [x[col.outcome]]);
sh.getRange(2, col.outcome+1, outcome.length, 1).setValues(outcome);
const days = rows.map(x => [x[col.days]]);
sh.getRange(2, col.days+1, days.length, 1).setValues(days);
}
// function returns number of days between two dates
function days_between(date1, date2) {
let msec = Math.abs(date1.getTime() - date2.getTime());
let days = Math.floor(msec/(1000*60*60*24));
return days;
}
随时为您的电子表格修改它。
潜在客户分析
我想要可以复制和粘贴的数据来测试此代码:
function leadAnalysis() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const sr = 2;//data start row
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 6).getValues();
let lo = { lA: [] };
vs.forEach((r, i) => {
if (!lo.hasOwnProperty(r[0])) {
lo[r[0]] = [];
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': 'New Sales', days_between_booked_on: 0 });
sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
lo.lA.push(r[0]);
} else {
let d = DiffInDays1(new Date(lo[r[0]][lo[r[0]].length - 1].oppt_booked_on), new Date(r[5]))
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': (d > 30) ? 'New Sales' : 'WinBack', days_between_booked_on: d });
sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
}
})
}
function DiffInDays1(Day1, Day2) {
if (Day1 && Day2 && (Object.prototype.toString.call(Day1) === '[object Date]') && (Object.prototype.toString.call(Day2) === '[object Date]')) {
var day = 86400000;
var t1 = new Date(Day1).valueOf();
var t2 = new Date(Day2).valueOf();
var d = Math.abs(t2 - t1);
var days = Math.floor(d / day);
//Logger.log(days);
return days;
} else {
throw 'Invalid Inputs';
}
}
通过自己输入得到如下数据如下:
lead_id | oppt_booked_on | Outcome | Days | ||||||
---|---|---|---|---|---|---|---|---|---|
id1 | 1/1/2021 | New Sales | 0 | ||||||
id2 | 1/2/2021 | New Sales | 0 | ||||||
id3 | 1/3/2021 | New Sales | 0 | ||||||
id4 | 1/4/2021 | New Sales | 0 | ||||||
id5 | 1/5/2021 | New Sales | 0 | ||||||
id6 | 1/6/2021 | New Sales | 0 | ||||||
id1 | 3/1/2021 | New Sales | 59 | ||||||
id2 | 3/2/2021 | New Sales | 59 | ||||||
id3 | 3/3/2021 | New Sales | 59 | ||||||
id4 | 3/4/2021 | New Sales | 59 | ||||||
id5 | 3/5/2021 | New Sales | 59 | ||||||
id6 | 3/6/2021 | New Sales | 59 | ||||||
id1 | 3/18/2021 | WinBack | 16 | ||||||
id2 | 3/19/2021 | WinBack | 16 | ||||||
id3 | 3/20/2021 | WinBack | 16 | ||||||
id4 | 3/21/2021 | WinBack | 16 | ||||||
id5 | 3/22/2021 | WinBack | 16 | ||||||
id6 | 3/23/2021 | WinBack | 16 |
这个版本会 运行 快得多:
function leadAnalysis() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const sr = 2;//data start row
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 6).getValues();
let lo = { lA: [] };
let oA = [];
vs.forEach((r, i) => {
if (!lo.hasOwnProperty(r[0])) {
lo[r[0]] = [];
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': 'New Sales', days_between_booked_on: 0 });
//sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
oA.push([lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on])
lo.lA.push(r[0]);
} else {
let d = DiffInDays1(new Date(lo[r[0]][lo[r[0]].length - 1].oppt_booked_on), new Date(r[5]))
lo[r[0]].push({ lead_id: r[0], lead_name: r[1], booked_by: r[2], cars_on_search: r[3], sales_segment: r[4], oppt_booked_on: r[5], digital_consulant: r[6], 'Outcome': (d > 30) ? 'New Sales' : 'WinBack', days_between_booked_on: d });
//sh.getRange(i + sr, 9,1,2).setValues([[lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]]);
oA.push([lo[r[0]][lo[r[0]].length-1].Outcome,lo[r[0]][lo[r[0]].length-1].days_between_booked_on]);
}
})
sh.getRange(sr,9,oA.length,oA[0].length).setValues(oA)
}