簡體   English   中英

如何使用 Google Apps 腳本將公式()設置為范圍?

[英]How to setFormulas() to a range using Google Apps Script?

我需要更改什么以便此腳本將公式(從第 2 行)設置為定義的范圍? 下面的代碼沒有給我任何錯誤,但它根本沒有設置公式。

  var lc = sheet.getLastColumn();
  var range = sheet.getRange(2, 1, 1, lc);
  var formulas = range.getFormulas();
  
  range.copyFormatToRange(sheet, 1, lc, 3, 3+response); //This pastes the format to the range, whose number of rows is defined depending on the # that the user inputs.

  var pasteTo = sheet.getRange(3, 1, response, lc);//Defines the range for setting formulas
  for (var a = 3; a < response; a++){
    pasteTo = sheet.getRange("A" + a,sheet.getLastColumn());
    pasteTo.setFormulas(formulas);
  }
  }
}

謝謝@TheMaster 的指導。 這是我解決它的方法:

  var lc = sheet.getLastColumn();
  var range = sheet.getRange(2, 1, 1, lc);
  var formulas = range.getFormulas();
  
  range.copyFormatToRange(sheet, 1, lc, 3, 3+response);

  var pasteTo = sheet.getRange(3, 1, response, lc);
  for (var a = 1; a <= response; a++){
    pasteTo = sheet.getRange(a+2,1,1,lc);
    pasteTo.setFormulas(formulas);
    }
  }
}

謝謝!

在 Arrays 中做所有事情要快得多

function kdkdkd() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const osh = ss.getSheetByName('Sheet1');
  const fA = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getFormulas();
  osh.getRange(row,col,fA.length,fA[0].length).setFormulas(fA);  
}

為 row 和 col 選擇任何你想要的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM