2017-03-18 130 views
-1

复制特定值并粘贴到Google Sheet中另一个工作表的特定单元格中我在“Sheet1”中具有以下结构中的表格。根据条件

enter image description here

Date   CQ % LR%  UD % DP % DSI % UB % DDD % 
26-Feb-2017 0.49% 0.15% 0.37% 0.53% 0.31% 0.82% 4.09% 
27-Feb-2017 0.51% 0.12% 0.46% 0.71% 0.36% 0.91% 4.06% 
28-Feb-2017 0.56% 0.18% 0.50% 0.72% 0.33% 0.93% 3.28% 
1-Mar-2017 0.62% 0.13% 0.42% 0.75% 0.34% 0.94% 5.08% 
2-Mar-2017 0.59% 0.12% 0.42% 0.76% 0.35% 0.99% 5.12% 
3-Mar-2017 0.62% 0.13% 0.50% 0.80% 0.32% 0.91% 5.33% 
4-Mar-2017 0.72% 0.22% 0.52% 1.49% 0.37% 1.08% 4.05% 
5-Mar-2017 0.68% 0.17% 0.43% 0.74% 0.35% 1.01% 4.76% 
6-Mar-2017 0.63% 0.18% 0.55% 0.88% 0.38% 1.02% 4.88% 
7-Mar-2017 0.56% 0.19% 0.41% 0.75% 0.33% 0.91% 4.76% 

在“表2”中从“工作表Sheet”值将在被复制并粘贴上述的特定的格式和条件。

Condition (>=) 

CQ % LR% UD%  DP% DSI% UB% DDD% Rating 
0.58% 0.17% 0.47% 0.75% 0.35% 0.93% 4.50% 4.57 

格式,它会被复制

Date  Metric Value 
27-02-2017 DSI  0.36% 
28-02-2017 CQ  0.56% 
28-02-2017 LR  0.18% 
28-02-2017 UD  0.50% 
28-02-2017 UB  0.93% 
01-03-2017 CQ  0.62% 
01-03-2017 DP  0.75% 
01-03-2017 UB  0.94% etc 
+0

表图像被附加在图片格式 –

+0

在你格式化输出的第二行你'28-02-2017 CQ 0.56%'但不是CQ的条件应该是> = 0.56% – Cooper

+0

是的条件将是> = 0.56%。这是我打字时从我身边犯的一个错误。请帮我解决这个问题。 –

回答

0

存储在一个关联数组

标准选择数据我创建了三个页的数据,标准和输出。我使用关联数组来保存“Criteria”表中的条件,并将关联数组的键用作“Data”表中数据表的标题。最后,我通过使用outputA数组的高度和必要的3列输出来获得最终输出范围的大小。

function listSelectedValues() 
{ 
    var ss=SpreadsheetApp.getActiveSpreadsheet(); 
    var data=ss.getSheetByName('Data'); 
    var crit=ss.getSheetByName('Criteria') 
    var dataA=data.getDataRange().getValues(); 
    var critA=crit.getDataRange().getValues(); 
    var dictA={}; 
    var outputA=[]; 
    var outputS=ss.getSheetByName('Output'); 
    for(var i=0;i<critA[0].length;i++) 
    { 
    dictA[critA[0][i]]=critA[1][i]; //this builds a criteria dictionary or lookup table 
    } 
    /* This is just debugging stuff to make sure I got the associative array correct and yes I did include the rating but it's not one of the columns so it never gets used. 
    var s='{'; 
    var firstTime=true; 
    for(var key in dictA) 
    { 
    if(!firstTime)s+=','; 
    else firstTime=false; 
    s+=key + ':' + dictA[key]; 
    } 
    s+='}' 
    dispStatus('dictA',s,800,600); 
    */ 
    for(var i=1;i<dataA.length;i++)//skip over the dates i starts at 1 
    { 
    for(var j=1;j<dataA[i].length;j++)//skip over the keys j starts at 1 
    { 
     if(dataA[i][j]>=dictA[dataA[0][j]])//this is possible because I used column headers that are keys in the associative array (dictA) 
     { 
     outputA.push([dataA[i][0],dataA[0][j],dataA[i][j]]); //notice the extra array bracket for inserting an entire row and insuring a 2 dimensional array for the coming setValues command. 
     } 
    } 
    } 
    var outputR=outputS.getRange(1, 1, outputA.length, 3); 
    outputR.setValues(outputA); 
} 

你可能会想修改此,这样就可以使标题行更有意义,你的观众,但随后将使用头元素作为键的关联数组的复杂化。只要有可能,我会选择阻力最小的路径。

我包括了我使用的dispStatus例程。

function dispStatus(title,html,width,height,modal) 
{ 
    var title = typeof(title) !== 'undefined' ? title : 'No Title Provided'; 
    var width = typeof(width) !== 'undefined' ? width : 400; 
    var height = typeof(height) !== 'undefined' ? height : 300; 
    var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>'; 
    var modal = typeof(modal) !== 'undefined' ? modal : false; 
    var htmlOutput = HtmlService 
    .createHtmlOutput(html) 
    .setWidth(width) 
    .setHeight(height); 
if(!modal) 
{ 
    SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title); 
} 
else 
{ 
    SpreadsheetApp.getUi().showModalDialog(htmlOutput, title); 
} 
} 

没有格式化的最终输出看起来像这样。

2/27/2017 DSI 0.36 
2/28/2017 LR 0.18 
2/28/2017 UD 0.5 
2/28/2017 UB 0.93 
3/1/2017 CQ 0.62 
3/1/2017 DP 0.75 
3/1/2017 UB 0.94 
3/1/2017 DDD 5.08 
3/2/2017 CQ 0.59 
3/2/2017 DP 0.76 
3/2/2017 DSI 0.35 
3/2/2017 UB 0.99 
3/2/2017 DDD 5.12 
3/3/2017 CQ 0.62 
3/3/2017 UD 0.5 
3/3/2017 DP 0.8 
3/3/2017 DDD 5.33 
3/4/2017 CQ 0.72 
3/4/2017 LR 0.22 
3/4/2017 UD 0.52 
3/4/2017 DP 1.49 
3/4/2017 DSI 0.37 
3/4/2017 UB 1.08 
3/5/2017 CQ 0.68 
3/5/2017 LR 0.17 
3/5/2017 DSI 0.35 
3/5/2017 UB 1.01 
3/5/2017 DDD 4.76 
3/6/2017 CQ 0.63 
3/6/2017 LR 0.18 
3/6/2017 UD 0.55 
3/6/2017 DP 0.88 
3/6/2017 DSI 0.38 
3/6/2017 UB 1.02 
3/6/2017 DDD 4.88 
3/7/2017 LR 0.19 
3/7/2017 DP 0.75 
3/7/2017 DDD 4.76 

我希望这对你有帮助。

好的,我在这个版本中加入了最后一个等式。

function listSelectedValues() 
{ 
    var ss=SpreadsheetApp.getActiveSpreadsheet(); 
    var data=ss.getSheetByName('Data'); 
    var crit=ss.getSheetByName('Criteria') 
    var dataA=data.getDataRange().getValues(); 
    var critA=crit.getDataRange().getValues(); 
    var dictA={}; 
    var outputA=[]; 
    var outputS=ss.getSheetByName('Output'); 
    for(var i=0;i<critA[0].length;i++) 
    { 
    dictA[critA[0][i]]=critA[1][i]; //this build a criteria dictionary or lookup table 
    } 
    for(var i=1;i<dataA.length;i++) 
    { 
    for(var j=1;j<dataA[i].length;j++) 
    { 

     if((j<dataA[i].length-1)&&(dataA[i][j]>=dictA[dataA[0][j]])||((j=dataA[i].length-1)&&(dataA[i][j]<dictA[dataA[0][j]]))) 
     { 
     outputA.push([dataA[i][0],dataA[0][j],dataA[i][j]]); 
     } 
    } 
    } 
    var outputR=outputS.getRange(1, 1, outputA.length, 3); 
    outputR.setValues(outputA); 
    var end = 'is near'; 
} 

这是我的测试数据。

Date CQ LR UD DP DSI UB DDD Rating 
26-Feb-2017 0.49 0.15 0.37 0.53 0.31 0.82 4.09 5.00 
27-Feb-2017 0.51 0.12 0.46 0.71 0.36 0.91 4.06 4.90 
28-Feb-2017 0.56 0.18 0.5 0.72 0.33 0.93 3.28 4.80 
1-Mar-2017 0.62 0.13 0.42 0.75 0.34 0.94 5.08 4.70 
2-Mar-2017 0.59 0.12 0.42 0.76 0.35 0.99 5.12 4.60 
3-Mar-2017 0.62 0.13 0.5 0.8 0.32 0.91 5.33 4.50 
4-Mar-2017 0.72 0.22 0.52 1.49 0.37 1.08 4.05 4.40 
5-Mar-2017 0.68 0.17 0.43 0.74 0.35 1.01 4.76 4.30 
6-Mar-2017 0.63 0.18 0.55 0.88 0.38 1.02 4.88 4.20 
7-Mar-2017 0.56 0.19 0.41 0.75 0.33 0.91 4.76 4.10 

这是我的输出

3/1/2017 CQ 0.62 
3/2/2017 CQ 0.59 
3/3/2017 CQ 0.62 
3/3/2017 Rating 4.50 
3/4/2017 CQ 0.72 
3/4/2017 LR 0.22 
3/4/2017 UD 0.52 
3/4/2017 DP 1.49 
3/4/2017 DSI 0.37 
3/4/2017 UB 1.08 
3/4/2017 Rating 4.40 
3/5/2017 CQ 0.68 
3/5/2017 LR 0.17 
3/5/2017 Rating 4.30 
3/6/2017 CQ 0.63 
3/6/2017 LR 0.18 
3/6/2017 UD 0.55 
3/6/2017 DP 0.88 
3/6/2017 DSI 0.38 
3/6/2017 UB 1.02 
3/6/2017 DDD 4.88 
3/6/2017 Rating 4.20 
3/7/2017 Rating 4.10 
+0

我收到下面提到的错误。范围的坐标或尺寸无效。 (第37行,文件“代码”)。请指导。 –

+0

我收集你的其他评论,你已经解决了这个问题。如果不是,那么请重新发布您的当前代码,以便我可以查看它,因为我无法重现该问题。然而,在处理setValues()命令时,通常会遇到范围大小问题。 – Cooper

+0

谢谢库珀。问题得到解决 –