How to change the Height of Multiple rows at Once?

2019-07-24 15:35发布

How do I change the height of multiple rows at once via Google Apps Script?

Attempt:

function resizeHeight() {

  var s = SpreadsheetApp,
      ui = s.getUi(),
      sh = s.getActiveSheet(),
      curntRow = s.getActiveRange().getRow(),
      rowsMany = s.getActiveRange().getNumRows(),
      autoRangeRows = s.getActiveRange().getA1Notation().replace(/[a-z]/gi,""),
      getVal = ui.prompt('⿱⇕', 'Change the height of row❓   Cancel. Back to default❗  (21)', ui.ButtonSet.OK_CANCEL),
      Btn = getVal.getSelectedButton(), Txt = getVal.getResponseText();

    for (var i=curntRow; i<=rowsMany+1; i++) {
      if (Btn == 'OK') {
         if (autoRangeRows.search(":") == -1) {sh.setRowHeight(curntRow, Txt);}
         else {sh.setRowHeight(i, Txt);}
      }
      if (Btn == 'CANCEL') {
         if (autoRangeRows.search(":") == -1) {sh.setRowHeight(curntRow, 21);}
         else {sh.setRowHeight(i, 21);}
      }
   }
}

Problem:

The script is not very effective they only work on the part or a few rows but not on all rows can be implemented.

1条回答
smile是对你的礼貌
2楼-- · 2019-07-24 16:16

This function works: You just have to pick an active range to select the rows that you want to change the height of and then give it a height in pixels in the prompt.

function changeRowHeight()
{
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sht=ss.getActiveSheet()
  var rng=sht.getActiveRange();
  var row=rng.getRow();
  var numrows=rng.getNumRows();
  var resp=SpreadsheetApp.getUi().prompt('Get Row Height', 'Enter Row Height in Pixels', SpreadsheetApp.getUi().ButtonSet.OK);
  var height = Number(resp.getResponseText());
  for(var i=0;i<numrows;i++)
  {
    sht.setRowHeight(row + i, height)
  }
}
查看更多
登录 后发表回答