再試一次。
function dealStage(e)
{
//var ss = SpreadsheetApp.getActiveSpreadsheet();
var ss = e.source;
var sht = ss.getSheetByName('ClientData');
var rng = sht.getDataRange();
var rngA = rng.getValues();
var clientheaders = rngA[0];
var dsSht = ss.getSheetByName('DealStage');
var dsShtRng = dsSht.getRange(2, 1, dsSht.getLastRow(), dsSht.getLastColumn());
dsShtRng.clearContent();
var dsShtHdrsRng = dsSht.getRange(1, 1, 1, dsSht.getLastColumn());
var dsShtHdrsRngA = dsShtHdrsRng.getValues();
var dsheaders = dsShtHdrsRngA[0];
var prelaunch = [];
var outreach = [];
var brochuresent = [];
var callbooked = [];
var dsi = clientheaders.indexOf('Deal Stage');
var ni = clientheaders.indexOf('Name');
for(var i=1;i<rngA.length;i++)
{
var dsv = rngA[i][dsi];
var nv = rngA[i][ni]
if(dsv!='')
{
switch(dsv)
{
case('Pre-Launch'):
prelaunch.push([nv]);
break;
case('Out-Reach'):
outreach.push([nv]);
break;
case('Brochure-Sent'):
brochuresent.push([nv]);
break;
case('Call-Booked'):
callbooked.push([nv]);
break;
}
}
}
var dsstartrow = dsSht.getLastRow()+1;
for(var i=0;i<dsheaders.length;i++)
{
var hdr = dsheaders[i];
switch(hdr)
{
case('Pre-Launch'):
if(prelaunch.length>0)dsSht.getRange(dsstartrow,i+1,prelaunch.length,1).setValues(prelaunch);
break;
case('Out-Reach'):
if(outreach.length>0)dsSht.getRange(dsstartrow,i+1,outreach.length,1).setValues(outreach);
break;
case('Brochure-Sent'):
if(brochuresent.length>0)dsSht.getRange(dsstartrow,i+1,brochuresent.length,1).setValues(brochuresent);
break;
case('Call-Booked'):
if(callbooked.length>0)dsSht.getRange(dsstartrow,i+1,callbooked.length,1).setValues(callbooked);
break;
}
}
SpreadsheetApp.flush();
}
ClientData表:
DealStage表:
驗證表:
感謝您的建議@Cooper。但是,只有將交易階段設置爲單個工作表時,該解決方案纔有效。在這種情況下,這是行不通的。我需要保留表1中的所有數據,並使用表2來表示「姓名」所在的「交易階段」。 – Bbrads