我只需要使用 Google Apps 脚本在电子表格中复制和粘贴公式
var range = activeSheet.getRange(targetRow-1, 1, 1, activeSheet.getLastColumn());
range.copyTo(activeSheet.getRange(targetRow, 1, 1, activeSheet.getLastColumn()), {contentsOnly:false});
这给了我完整的信息,包括公式和数据。但是,我只想复制公式(没有数据)。
CopyFormulasToRange
不幸的是不存在。
以下函数将仅复制公式。它具有用于开始获取公式的行和列的用户设置。
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句