编码以组织数据

时间:2017-09-09 14:27:30

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

所以我在电子表格中有大约2000个条目,其中包含一些公司的数据。数据按如下方式组织:

  

公司名称:...
电子邮件:.......
电话号码:.....
网站:....
地址: ....

公司名称:...
电子邮件:...
电话号码:...

我想要的是编写一个在列

下的另一个电子表格中组织此功能的函数
  

公司名称电子邮件电话号码Web
........... .........

我写了函数的介绍

function OrganizeData()
{var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Orginial");
 var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Organized");

但是我不知道如何编写一个代码,将某个关键字(电子邮件)之后的文本从电子表格的一行复制到电子表格中的另一列。

1 个答案:

答案 0 :(得分:0)

这有点猜测,但将垂直定位的数据转换为列。

function testGetData()
{
  getData('SpreadSheetID','Sheet1','Sheet2'); 
}

function getData(spreadsheetId,inputSheetName,outputSheetName)
{
  var ss=SpreadsheetApp.openById(spreadsheetId);
  var inpsh=ss.getSheetByName(inputSheetName);
  var outsh=ss.getSheetByName(outputSheetName);
  var inrg=inpsh.getDataRange();
  var invA=inrg.getValues();
  var outA=[];
  var n=0;
  outA.push(['Name','Email','Phone','Web','Address','Error']);
  var row=[];
  for(var i=0;i<invA.length;i++)
  {
    var vA=[];
    vA=String(invA[i][0]).split(':');//assuming data is in one string
    if(vA.length<2)//assuming data is split into first two columns with no : in the string
    {
      vA.push(invA[i][1]);
    }
    switch(vA[0])
    {
      case 'Company name':
        row['Name']=String(vA[1]).trim();//trim off whitespace both ends
        break;
      case 'Email':
        row['Email']=String(vA[1]).trim();
        break
      case 'Phone no':
        row['Phone']=String(vA[1]).trim();
        break;
      case 'Web':
        row['Web']=String(vA[1]).trim();
        break;
      case 'address':
        row['Address']=String(vA[1]).trim();
        outA.push([(typeof(row.Name)!='undefined')?row.Name:'',(typeof(row.Email)!='undefined')?row.Email:'',(typeof(row.Phone)!='undefined')?row.Phone:'',(typeof(row.Web)!='undefined')?row.Web:'',(typeof(row.Address)!='undefined')?row.Address:'',(typeof(row.Error)!='undefined')?row.Error:'']);
        break;
      default:
        row['Error']=Utilities.formatString('Row Error: Index: %s vA[0]= %s vA[1]= %s',i,(typeof(vA[0])!='undefined')?vA[0]:'Not Defined',(typeof(vA[1])!='undefined')?vA[1]:'Not Defined');
        break;
    }
  }
  var otrg=outsh.getRange(1,1,outA.length,outA[0].length);//determine size of output array Its good to have headers for this
  otrg.setValues(outA);
}

我的测试数据表:

enter image description here

输出表:

enter image description here

错误:指示先前数据行的问题。

进行了一些更改,使其出现问题的可能性稍小。

function getData(spreadsheetId,inputSheetName,outputSheetName)
{
  var ss=SpreadsheetApp.openById(spreadsheetId);
  var inpsh=ss.getSheetByName(inputSheetName);
  var outsh=ss.getSheetByName(outputSheetName);
  var inrg=inpsh.getDataRange();
  var invA=inrg.getValues();
  var outA=[];
  var n=0;
  outA.push(['Name','Email','Phone','Web','Address','Error']);
  var row=[];
  for(var i=0;i<invA.length;i++)
  {
    var vA=[];
    vA=String(invA[i][0]).split(':');//assuming data is in one string
    if(vA.length<2)//assuming data is split into first two columns with no : in the string
    {
      vA.push(invA[i][1]);
    }
    switch(vA[0])
    {
      case 'Company name':
        row['Name']=String(vA[1]).trim();//trim off whitespace both ends
        break;
      case 'Email':
        row['Email']=String(vA[1]).trim();
        break
      case 'Phone no':
        row['Phone']=String(vA[1]).trim();
        break;
      case 'Web':
        row['Web']=String(vA[1]).trim();
        break;
      case 'address':
        row['Address']=String(vA[1]).trim();
        outA.push([(typeof(row.Name)!='undefined')?row.Name:'',(typeof(row.Email)!='undefined')?row.Email:'',(typeof(row.Phone)!='undefined')?row.Phone:'',(typeof(row.Web)!='undefined')?row.Web:'',(typeof(row.Address)!='undefined')?row.Address:'',(typeof(row.Error)!='undefined')?row.Error:'']);
        row['Name']='';//cleared out the row array
        row['Email']='';
        row['Phone']='';
        row['Address']='';
        row['Error']='';
        break;
      default:
        row['Error']=Utilities.formatString('Row Error: Index: %s vA[0]= %s vA[1]= %s',i,(typeof(vA[0])!='undefined')?vA[0]:'Not Defined',(typeof(vA[1])!='undefined')?vA[1]:'Not Defined');
        outA.push(['','','','','',row.Error]);//output errors on separate lines
        row['Error']='';//cleared errors
        break;
    }
  }
  var otrg=outsh.getRange(1,1,outA.length,outA[0].length);//determine size of output array Its good to have headers for this
  otrg.setValues(outA);
}

修改数据有一些差异:

enter image description here

这是新输出

enter image description here