2017-05-23 9 views
1

私がGoogle Appsスクリプトを使用してのみ、スプレッドシートに数式をコピー&ペーストする必要がGoogle Appsのスクリプト:コピーと貼り付け式のみ

var range = activeSheet.getRange(targetRow-1, 1, 1, activeSheet.getLastColumn()); 
range.copyTo(activeSheet.getRange(targetRow, 1, 1, activeSheet.getLastColumn()), {contentsOnly:false}); 

これは私に数式やデータを含む完全な情報を与えます。しかし、私は数式(データなし)をコピーするだけです。

CopyFormulasToRange残念ながら存在しません。

答えて

3

次の関数は、数式のみをコピーします。数式を取得する行と列のユーザー設定があります。

function copyFormulas() { 
    var activeSheet,numberOfSourceColumnsToGet,sourceColumnStart,sourceFormulas,sourceRange, 
     sourceRowStart,targetColumn,targetRange,targetRowStart; 

    //USER INPUT 

    sourceRowStart = 1; //Row to start getting formulas from 
    sourceColumnStart = 2; //Column to start getting formulas from 
    numberOfSourceColumnsToGet = 1; //Number of columns to get formulas from 

    targetRowStart = 1; //Row to start copying formulas to 
    targetColumn = 3; //Column to start copying formulas to 

    //END OF USER INPUT 

    activeSheet = SpreadsheetApp.getActiveSheet(); 
    sourceRange = activeSheet.getRange(sourceRowStart, sourceColumnStart, activeSheet.getLastRow(), numberOfSourceColumnsToGet); 

    sourceFormulas = sourceRange.getFormulas();//Get only formulas from the source range 

    targetRange = activeSheet.getRange(targetRowStart,targetColumn,sourceFormulas.length,sourceFormulas[0].length); 

    targetRange.setFormulas(sourceFormulas);//Copy the formulas to the target range 
}