复制时展开公式

时间:2019-06-13 09:29:42

标签: google-apps-script google-sheets

该脚本将所有配方设计师复制粘贴到右侧的一列,但是该公式也必须在右侧扩展。

示例:A1 + A3-> B1 + B3

现在,运行sript时,公式只是重复了

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

  //USER INPUT

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

  targetRowStart = 1; //Row to start copying formulas to
  targetColumn = 5; //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
}

1 个答案:

答案 0 :(得分:0)

正如@АлександрЕрмолин所说,您使用了错误的命令。试试这个:

function so5657743702() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheetname = "56577437";
  var sheet = ss.getSheetByName(sheetname);
  var sourcerange = sheet.getRange("A4");
  var targetrange = sheet.getRange("B4");
  sourcerange.copyTo(targetrange, SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
}