1

を変換することはできません私は、Googleのシートで指定されたカラムを通るでしょうGoogleのスクリプト:別のシートに行をコピーしようとすると、エラー

  1. 行で行く行機能に取り組んでいます(列Hが値として示されている[7 ])。
  2. 特定の値( "YES")を持つ値[7]列のセルに基づいて、行全体が選択されます。
  3. これで、選択された行(その全体)が同じドキュメント内の別のシートにコピーされます。

    master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange); 
    

    エラー状態:

    Cannot convert [here the Logger error provides a list of all values in a row separated by a comma] to (class). 
    

    function moveRowsBasedOnCellValue(){ 
    
        var ss = SpreadsheetApp.getActiveSpreadsheet(); 
        var sheet = ss.getActiveSheet(); 
        var master = ss.getSheetByName('Main'); 
        var values = master.getDataRange().getValues(); //selects all values in a sheet 
    
        values.forEach(function(value){ 
        //if cell in the column H equals YES execute the script 
        if (value[7] == "YES"){ 
         //variable to select all columns in the source sheet that aren't blank 
         var colWidth = master.getMaxColumns(); 
         //variable containing the target sheet 
         var destinationYeses = ss.getSheetByName("Yeses"); 
         //select first empty row in the destination sheet 
         var destinationYesesRange = destinationYeses.getRange(destinationYeses.getLastRow()+1,1); 
         //copy the relevant row into the sheet 
         master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange); 
        }}); 
    } 
    

    スクリプトは、スクリプトの最後の行まで罰金実行:

このは、私がこれまで持っているものです

私は何が間違っているかもしれないと思っていますか?

答えて

2

あなたがそのエラーを取得している理由は、次のコード

master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange); 

にgetRangeがそれに渡されたすべての値が整数であることを期待しています。 value変数は配列ですが、

function moveRowsBasedOnCellValue(){ 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 
    var master = ss.getSheetByName('Main'); 
    var values = master.getDataRange().getValues(); //selects all values in a sheet 

    values.forEach(function(value,index){ 
    //if cell in the column H equals YES execute the script 
    if (value[7] == "YES"){ 
     //variable to select all columns in the source sheet that aren't blank 
     var colWidth = master.getMaxColumns(); 
     //variable containing the target sheet 
     var destinationYeses = ss.getSheetByName("Yeses"); 
     //select first empty row in the destination sheet 
     var destinationYesesRange = destinationYeses.getRange(destinationYeses.getLastRow()+1,1); 
     //copy the relevant row into the sheet 

     master.getRange(index +1 ,1, 1, colWidth).copyTo(destinationYesesRange); 
    }}); 
} 
:だからあなたの最終的なコードは次のようになります

//Index for array(values) starts at 0, whereas for the rows number in sheet starts at 1. 
//so add 1 to convert the index to row number 
master.getRange(index + 1,1, 1, colWidth).copyTo(destinationYesesRange); 

Documentation

values.forEach(function(value,index){ // gives the array index to index variable 

第二:だから、それを修正するために、あなたは

まず、次の2つの変更を加える必要があります

最終的な注記:これは、データを転送する非常に非効率的な方法であり、実行コピーするデータが大量でスクリプトの実行に5-6分以上かかる場合は、終了してください。この投稿を確認してくださいaddresses this issue

2

これはあなたのためにそれを行います。

function copyYesToYeses() 
{ 
    var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('YesToYeses'); 
    var rng = sht.getDataRange(); 
    var yesesSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Yeses'); 
    var rngA = rng.getValues(); 
    var yesesA = []; 
    for(var i=0;i<rngA.length;i++) 
    { 
    if(rngA[i][2]=='Yes') 
    { 
     yesesA.push(rngA[i]) 
    } 
    } 
    var yesesRng = yesesSht.getRange(1, 1, yesesA.length, yesesA[0].length); 
    yesesRng.setValues(yesesA); 
} 

これは自分のデータシートの画像です。

enter image description here

これは、すべてのyesesを含むシートです。

enter image description here

を役に立てば幸い
関連する問題