Get data next to eachother

时间:2018-12-19 11:17:43

标签: google-apps-script google-sheets gmail

I've made a script for sending bulk emails from google sheet. However I stumbled across a problem. For some reason I'm not able to get the data below next to each other.

var open = row[1] + row[2] + row[3]

Beneath you will find a piece of the google sheet list. I use a vlookup on ID number to insert that data next to the message of the pupil/mail adress.

In my message I have an introduction (hello pupil, etc.) After, they get (or should get) a list of their points.

var message = introduction + '\n\n' + open  + '\n\n\n' + ending; // setup of the mail

However, when they receive the mail it looks like this

Sarah
Sarah
Chemistry
Maths
9,5
6,0

Is there a way to get this data next to each? like \n for new paragraph.

Thanks in advance

Edit: It should look like this.

Sarah     Maths         9,5
Sarah     Chemistry     6,0

Edit2:

Macro i use to merge columns

function merge_col() {
  var ss = SpreadsheetApp.getActive().getSheetByName('Open invoices');
  var data = ss.getDataRange().getValues();
  var header = data.shift();
  var col1 = data[1][0];
  var col2 = '';
  var col3 = '';
  var col4 = '';
  var dataSheet2 = [header];
  for(var i=0 ; i<data.length ; i++){
    Logger.log(data[i][0]+'  -  '+col1+'  -  '+data[i][2])
    if(col1==data[i][0]){
      col1 = data[i][0];
      col2+=data[i][1]+'\n';
      col3+=data[i][2]+'\n';
      col4+=data[i][3]+'\n';
    }else{
      dataSheet2.push([col1,col2,col3,col4]); 
      var col1 = data[i][0];
      col2 = data[i][1]+'\n';
      col3 = data[i][2]+'\n';
      col4 = data[i][3]+'\n';
    }
  }
  dataSheet2.push([col1,col2,col3,col4]); 
  Logger.log(dataSheet2);
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
  sheet2.clear();
  sheet2.getRange(1, 1, dataSheet2.length, dataSheet2[0].length).setValues(dataSheet2);
}

1 个答案:

答案 0 :(得分:0)

拼接行

代码:

function splicingRows(){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  for(var i=0;i<vA.length;i++){
    var maxrh=1;
    for(var j=0;j<vA[i].length;j++){//calculate max needed row height to fit all strings that are delimited with linefeeds
      if(vA[i][j]){
        var rh=vA[i][j].toString().split('\n').length;
        if(rh>maxrh){
          maxrh=rh;
        }
      }
    }
    var r=maxrh;
    if(r>1){//splice in the number of extra rows to fit the extra strings
      while(r>1){
        var vB=vA[i].slice();//dont forget the slice so that they are not the same array they need to be copies
        vA.splice(i,0,vB);
        r--;
      }
      r=maxrh;
      while(r>0){
        var k=maxrh-r
        for(var j=0;j<vA[i].length;j++){
          if(vA[i+k][j].toString().split('\n').length>1){//if there is more than one string then adjust the contents
            if(r<=vA[i+k][j].toString().split('\n').length){
              vA[i+k][j]=vA[i+k][j].toString().split('\n')[r-1];
            }else{
               vA[i+k][j]=null;
            }
          }else{
            vA[i+k][j]=vA[i+k][j];//if there is only one then just copy it.
          }
        }
        r--;
      }
    }

  }
  sh.getRange(1,1,vA.length,vA[0].length).setValues(vA);//replace the entire page all at one time
}

之前:

enter image description here

之后:

enter image description here

相关问题