4

私は Google スプレッドシートを持っていますが、多数の行を選択するために必要なコードを作成するのに非常に苦労しています。

選択したい唯一の行は、列の1つ(列Bとしましょう)に値または式がある行です。

列ごとにシートを並べ替えることができず、最初の x 値のみを選択します

シートのすべての行をループして、列 B が空かどうかを判断する必要があると考えていますが、これを行う方法や、セルに値または数式が含まれているかどうかを確認する方法がわかりません。数式が含まれている場合は、値より優先して保持する必要があります。

Google Apps スクリプトを使用して、特定の列の値または数式が空でない行のみを Google スプレッドシートから選択するにはどうすればよいですか?

4

2 に答える 2

4

これはあなたが望むことをするはずです...あなたが本当に欲しいものがわからないのでテストしなかったので、デバッグが必要かもしれません;-)

function copynotempty(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = SpreadsheetApp.setActiveSheet(ss.getSheets()[0])
  var col = 1 ; // choose the column you want to check: 0 = col A, 1= col B ...
  var range = sh.getDataRange();
  var values=range.getValues();// data is a 2D array, index0 = col A
  var formulas=range.getFormulas();// data is a 2D array, index0 = col A
  var target=new Array();// this is a new array to collect data
   for(n=0;n<range.getHeight();++n){
     if (values[n][col]!=''|| formulas[n][col]!=''){ ; 
        for (cc=0;cc<range.getWidth();++cc){
                if (formulas[n][cc]!=''){target[n][cc]=formulas[n][cc]}else{target[n][cc]=values[n][cc]}
    // if the cell has a formula copy it or else copy the value, do that for the whole row
// (this also defines and apply the 'priority' you mentioned in your question, I wasn't sure if it should apply to the whole row or only on column B)
                }
              }
            }
            if(target.length>0){// if there is something to copy
          var sh2=SpreadsheetApp.setActiveSheet(ss.getSheets()[1]); //second sheet of your spreadsheet
          sh2.getRange(1,1,target.length,target[0].length).setValues();// paste the selected values in the 2cond sheet
          }
        }
于 2012-06-23T12:37:26.353 に答える
1

これらの行で何かを試してください - このコードはテストされていませんが、かなりのアイデアが得られます

var sheet = // get sheet ; 
var data = sheet.getDataRange().getValues(); 
for ( var i = 0; i < data.length ; i++){
  if (data[i][1] != ''){  // Column B is at index 1
    // Do whatever you want
  }
}
于 2012-06-23T11:33:59.043 に答える