2014-09-29 2 views
1

Google 스프레드 시트에서 내 시트 중 하나의 데이터를 가져 오는 Martin Hawksey의 Google Apps Script 수정 된 버전을 사용하고 있습니다. 다른 시트가 각 시트에 대한 새 행을 만듭니다 이벤트 설명 열 (쉼표로 구분됨 - 예 : 셀 D2 = "설명 1, 설명 2, 설명 3 등).이 기능은 매우 훌륭하지만 한 가지 더 많은 기능을 수행하고 싶습니다. (아마도 쉼표 대신 세미콜론으로 분리 된) 이벤트 설명의 마지막 항목을 가져 와서 세미콜론이나 다른 구분 기호를 삭제하는 동안 고유 한 이벤트마다 한 번만 인접 셀에 밀어 넣습니다. 여러 가지 접근 방식이 있지만 Google Apps Script를 처음 사용하는 경우 성공하지 못했습니다.Google 스프레드 시트 셀 값을 인접한 셀로 분할하는 방법

function splitColumnAndRepeatRows(anArray, splitColumnIndex) { 
    var output = []; 
    for (i in anArray){ // for each row 
    var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column 
    for (j in splitArray){ // for each split cell value 
     if(splitArray[j]=="" && j>=1) 
     continue; 
     var row = anArray[i].slice(0); // take a copy of source row 
     row[splitColumnIndex] = alltrim(splitArray[j]); // replace comma separated value with current split value 
     output.push(row); // push new row to output 
    } 
    } 
    return output; 
} 

function alltrim(str) { 
    return str.replace(/^\s+|\s+$/g, ''); 
} 

는 기본적으로이 내가 할 노력하고있어입니다 -이 설정이로

Date  Client  County  Description 
9/21/14 12345  Greene  Location 1, Location 2, Location 3; Y 
9/24/14 54321  Brown  Location 1, Location 2; X 

을 :

Date  Client  County  Description  Time 
9/21/14 12345  Greene  Location 1 
9/21/14 12345  Greene  Location 2 
9/21/14 12345  Greene  Location 3  Y 
9/24/14 54321  Brown  Location 1 
9/24/14 54321  Brown  Location 2  X 

어떤 도움도 대단히 감사하겠습니다!

감사합니다.

답변

1

편집 된 splitColumnAndRepeatRows() 함수는 열의 쉼표로 구분 된 항목이 모두 구분 된 목록이 될 수있는 추가 보너스와 함께 사용자가 수행 한 작업을 처리하며 2로 분리됩니다 세포.

function splitColumnAndRepeatRows(anArray, splitColumnIndex) { 
    var output = []; 
    for (var i in anArray){ // for each row 
    var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column 
    for (var j in splitArray){ // for each split cell value 
     if(splitArray[j]=="" && j>=1) 
     continue; 
     var row = anArray[i].slice(0); // take a copy of source row 
     var trimmedString = alltrim(splitArray[j]); 
     var subArray = trimmedString.split(";"); // split current item of specified column at ; 
     if (subArray.length > 1) { // if current item is a ;-delimited list (now split into an array) 
     row[splitColumnIndex] = alltrim(subArray[0]); // replace comma-separated value with first item of ;-delimited array 
     row[splitColumnIndex+1] = alltrim(subArray[1]); // append second item of ;-delimited list as new cell to row 
     } 
     else { 
     row[splitColumnIndex] = trimmedString; // replace comma separated value with current split value 
     row[splitColumnIndex+1] = ""; // append empty cell to row 
     } 
     output.push(row); // push new row to output 
    } 
    } 
    return output; 
} 

그것은 이런 식으로 뭔가를 설정할 수 있습니다 :

9/21/14 12345  Greene  Location 1; A, Location 2, Location 3; B 
9/24/14 54321  Brown  Location 1, Location 2; C 

을이 속으로 : 마법처럼

9/21/14 12345  Greene  Location 1  A 
9/21/14 12345  Greene  Location 2 
9/21/14 12345  Greene  Location 3  B 
9/24/14 54321  Brown  Location 1 
9/24/14 54321  Brown  Location 2  C 
+0

작품! 도와 주셔서 정말 감사합니다! –

관련 문제