2017-10-25 4 views
0

にテキスト(区切り文字「(スペース)」)とシートに別々の列に2スプリット私はテキスト分割したい列appscript機能

を貼り付けるのSheet1の現在のフォーム私の出力はシート2のように見える: output

また、テキストの前後のテキストを "Hello"から削除したい。私はgetValues.split( "")で成功しましたが、宛先範囲に貼り付けることができませんでした(sheet2)。

function myFunction() { 
    var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); 
    var lr = ss1.getLastRow(); 
    var ss2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2"); 

    for (var i=2;i<=lr;i++) { 

    var title = ss1.getRange(i, 1).getValue().split(" "); 
    ss2.getRange(i, 2).setValue(title[0]); 

    } 

} 

助けが得られれば幸いです。

P.S.私は初心者であり、限られた知識(まだ学んでいる)を持っている欲しいjavascriptとgoogleのappscriptコーダーです。

TIA、歓声

答えて

0

以下のコードを試してみてください。それは正確に何をしたいのか分からないので、あなたの見出しは含まれていません。それはあなたの文字列から "こんにちは"単語を取り除き、すべての単語を別々の列に入れます。コードのコメントは私が何かしている理由を説明します。

function myFunction() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); // Selects the spreadsheet 
    var ss1 = ss.getSheetByName("Sheet1"); // Selects sheet 1 
    var data = ss1.getDataRange().getValues(); // Gets all values in the sheet 
    var ss2 = ss.getSheetByName("Sheet2"); // Selects sheet 2 

    for (var i=0;i<data.length;i++) { 
    var words = data[i][0].split(" "); // Create an array of all words 
    if (words[0].toLowerCase().indexOf('hello') > -1) { // True when the first word of the sentence contains hello 
     words.splice(0, 1); // Remove 1 item, starting from position 0 - remove the word that contains hello 
    } 
    // Get range on Sheet2, same row number as sheet 1, starting from column 1, select 1 row, and the same amount of columns as the amount of words 
    // Because we set values for a range that can span multiple rows, we put the words array in another array (where the words array represents one row) 
    // We add 1 to variable i as the index of an array starts at 0 instead of 1. 
    ss2.getRange(i+1, 1, 1, words.length).setValues([words]) 
    } 
} 

Tested in this document

関連する問題