简体   繁体   中英

Javascript HideRows (Google Sheet Script Editor)

I am trying to hide all the row of a specific Sheet in a Google speadsheet (the spreasheet contains others sheets). The rows to be hidden are the one with an ID between two intergers given by a two prompt commands (first_r and last_r).

When I click on the button hide , I get the two prompt window, but then a "Finished script" but the row aren't hidden.
Here is my code

function hide(){
var ui = SpreadsheetApp.getUi();
var first_r_str = ui.prompt('First row to hide');
var last_r_str = ui.prompt("Last row to hide");
 var first_r = parseInt(first_r_str);
 var last_r = parseInt(last_r_str);   

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('MYSheet');

for (i = first_r; i <= last_r; i++){
sheet.hideRows(i); 
}

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('Hide Rows', 'hide')
      .addToUi();
}

My code is in the script editor(from the spreadsheet top menu, select tools and then script editor)

Below code works perfectly fine, you missed to get the text value properly and can use the range.

Source Google Apps Script

function hide(){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var startRow = ui.prompt('enter first').getResponseText();
var endRow = ui.prompt('enter last').getResponseText();
var range = sheet.getRange(startRow, 1, endRow);
ss.hideRow(range);

}

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('Hide Rows', 'hide')
      .addToUi();
}

As MGA shows in his example the main problem is that you did not extract the text from the answer.

Since you have a consecutive range you should do one call to hideRows rather than looping, that is more efficient.

function hide(){
  var ui = SpreadsheetApp.getUi();
  var first_r_str = ui.prompt('First row to hide').getResponseText();
  var last_r_str = ui.prompt("Last row to hide").getResponseText();
  var first_r = parseInt(first_r_str);
  var last_r = parseInt(last_r_str);   

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');

  sheet.hideRows(first_r, last_r - first_r + 1); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM