Google apps script 从Google应用程序脚本中获取范围

Google apps script 从Google应用程序脚本中获取范围,google-apps-script,google-sheets,Google Apps Script,Google Sheets,有没有办法从范围中获取子范围 即 我可以从全范围中获取范围吗 上下文是,我正在使用数据验证根据范围动态设置数据验证下拉列表 示例:我有两个单元格-第一个是包含类别列表的下拉列表,第二个是包含子类别列表的下拉列表,子类别列表依赖于在第一个单元格中选择的类别 我实现这一点的方式是基于类别选择,我使用基于该类别选择的子类别列表填充一个隐藏行。然后我使用requirewainerange设置该子类别单元格的数据验证 它工作得很好,只是跑得非常慢,我正在想办法让它跑得更快。我猜它慢的原因之一是因为我在循环

有没有办法从范围中获取子范围

我可以从
全范围
中获取范围吗

上下文是,我正在使用数据验证根据范围动态设置数据验证下拉列表

示例:我有两个单元格-第一个是包含类别列表的下拉列表,第二个是包含子类别列表的下拉列表,子类别列表依赖于在第一个单元格中选择的类别

我实现这一点的方式是基于类别选择,我使用基于该类别选择的子类别列表填充一个隐藏行。然后我使用
requirewainerange
设置该子类别单元格的数据验证

它工作得很好,只是跑得非常慢,我正在想办法让它跑得更快。我猜它慢的原因之一是因为我在循环中使用
getRange
,以获得正确的
requireRequireWainRange
。所以我尝试拉取一个子范围,而不是每次都重新查询这个范围

function setDataValidations() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var categoryRange = ss.getRange("A:A");
  var subCatCells = ss.getRange("B:B");

  var subCatRules = subCatCells.getDataValidations();

  var rangeLength = categoryRange.getValues().length;

  for (var i = 1; i < rangeLength; i++ ){
    var catCell = categoryRange.getCell(i, 1);
    var subCatOptions = ss.getRange("'subcats'!A" + i + ":P" + i);
    var subCatRule = SpreadsheetApp.newDataValidation().requireValueInRange(subCatOptions, true).build();
  }
  catCells.setDataValidations(subCatRules);
}
函数setDataValidations(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var categoryRange=ss.getRange(“A:A”);
var subCatCells=ss.getRange(“B:B”);
var subactures=subCatCells.getDataValidations();
var rangeLength=categoryRange.getValues().length;
对于(变量i=1;i
是一个人如何从现有的
范围内引用
范围
,因此你已经在做你要求做的事情了。正如您所注意到的,使用优于逐单元设置,因为它是一种批处理方法。鉴于您使用了验证规则,因此无法避免获取额外的
Range
s

然而,对于有明确关系的特定用例,可以通过使用s更有效地获取它们。
范围列表
是批量获取的
范围
,通常用于对不相交的范围进行同等处理的用途

function setDVs() {
  const wb = SpreadsheetApp.getActive();
  const catSheetName = "the name of the sheet that has the dropdowns",
      sheet = wb.getSheetByName(catSheetName),
      maxDVs = sheet.getLastRow();

  // Create the A1-notation or R1C1-notation Arrays identifying the ranges we need.
  const subCatNotations = [];
  const subCatSheetName = "the name of the sheet that has the ranges with required values"
  for (var r = 1; r <= maxDVs; ++r)
    subCatNotations.push("A" + r + ":P" + r); // 1 row, cols A:P

  // Range#setDataValidations requires an Array of Array of DataValidations.
  // Thus, wrap the new rule in a new array if updating a single column.
  const new_dvs = wb.getSheetByName(subCatSheetName)
      .getRangeList(subCatNotations).getRanges()
      .map(function (subCatRange) {
        return [
          SpreadsheetApp.newDataValidation().requireValueInRange(subCatRange, true).build()
        ];
      });

  // Batch-apply the new rules to the `catSheetName` sheet.
  if (new_dvs.length && new_dvs[0].length)
    sheet.getRange(1, 1, new_dvs.length, new_dvs[0].length).setDataValidations(new_dvs); // col A
}
函数setDVs(){
const wb=SpreadsheetApp.getActive();
const catSheetName=“具有下拉列表的工作表的名称”,
sheet=wb.getSheetByName(catSheetName),
maxDVs=sheet.getLastRow();
//创建A1表示法或R1C1表示法数组,确定我们需要的范围。
常数子旋转=[];
const subCatSheetName=“具有所需值范围的工作表的名称”

对于(var r=1;r,下面的示例将获得由所有行和一些列组成的子范围

function getRangeByCols( myRange,  fromCol,  toCol){
  firstRowIndex = myRange.getRow()
  firstColIndex = myRange.getColumn()
  rowsNumber = myRange.getHeight()
  colsNumber = myRange.getWidth()

  return myRange.getSheet().getRange(firstRowIndex,firstColIndex+fromCol-1,rowsNumber,toCol-fromCol+1)
}

我创建了一个简单的函数,根据数据的大小用数据填充给定的
范围

/**
*使用提供的数据填充范围(二维阵列)
*不考虑列表中的行数/列数
*资料
**/
函数填充范围(范围、数据){
range.clear({contentsOnly:true})//可选
const target=range.getSheet().getRange(
range.getRow(),range.getColumn(),
data.length,数据[0].length)
target.setValues(数据)
}

使用
范围列表
s实现了这一技巧…从约300行的5分钟到5000行的1分钟。谢谢!
function getRangeByCols( myRange,  fromCol,  toCol){
  firstRowIndex = myRange.getRow()
  firstColIndex = myRange.getColumn()
  rowsNumber = myRange.getHeight()
  colsNumber = myRange.getWidth()

  return myRange.getSheet().getRange(firstRowIndex,firstColIndex+fromCol-1,rowsNumber,toCol-fromCol+1)
}