根据单元格中的第一个字符隐藏Google Spreadsheet中的行 [英] Hide Rows in a Google Spreadsheet based on first Character in a cell

查看:196
本文介绍了根据单元格中的第一个字符隐藏Google Spreadsheet中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Google Spreadsheet添加功能。电子表格从网络导入数据,并且我已将它们格式化得很好。我的一列是一系列字符串,以两种方式之一格式化 - 字符串或*字符串*没有空格(基本上从网上输入斜体)。

I我试图想出一个脚本,当我打开我的电子表格时,它会:


  • 取消隐藏电子表格中的所有行
  • >
  • 循环显示电子表格

  • 隐藏列2(B)以星号开头的每一行



我有以下几种:

pre $ function $ on $ {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(DoDH);
sheet.showRows(1,sheet.getMaxRows());
for(var i = 1; i< sheet.getMaxRows()+ 1; ++ i){
if(sheet.getRange(i,2).getValue()。){
sheet.hideRow(i)
}
}
}

我不知道如何访问每个单元格内的字符串,以及如何访问字符串中的字符。感谢您提前给予任何帮助。 这里是更新后的代码。请参阅评论以了解无洞洞察

 函数onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(DoDH);
var maxRows = sheet.getMaxRows();

//显示所有行
sheet.showRows(1,maxRows);

//从数据库中获取数据B
var data = sheet.getRange('B:B')。getValues();

//遍历所有行
for(var i = 0; i< data.length; i ++){
//比较第一个字符,如果是星号,则隐藏行
if(data [i] [0] .charAt(0)=='*'){
sheet.hideRows(i + 1);
}
}
}


I am working on adding functionality to a Google Spreadsheet. The spreadsheet imports data from the web, and I have it all formatted nicely. One of my columns is a series of strings, formatted in one of two ways - String or *String * without the space (basically importing italics from the web).

I am trying to come up with a script that runs when I open my spreadsheet, that will:

  • Unhide all rows in my spreadsheet
  • Loop through the spreadsheet
  • Hide every row where column 2(B) begins with an Asterisk

I have the following:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("DoDH");
  sheet.showRows(1, sheet.getMaxRows());
  for(var i=1; i<sheet.getMaxRows()+1; ++i){
    if (sheet.getRange(i, 2).getValue().){
      sheet.hideRow(i)
    }
  }
}

I don't know how to access the string inside each cell, and how to access characters within the string. Thank you in advance for any help.

解决方案

Here is updated code. See comments to have na insight

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("DoDH");
  var maxRows = sheet.getMaxRows();

  //show all the rows
  sheet.showRows(1, maxRows);

  //get data from clumn B
  var data = sheet.getRange('B:B').getValues();

  //iterate over all rows
  for(var i=0; i< data.length; i++){
    //compare first character, if asterisk, then hide row
    if(data[i][0].charAt(0) == '*'){
      sheet.hideRows(i+1);
    }
  }
}

这篇关于根据单元格中的第一个字符隐藏Google Spreadsheet中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆