简体   繁体   English

Google表格脚本:根据单元格值设置行格式-尝试优化脚本

[英]Google Sheets Script: Format Row Based on Cell Value - Trying to Optimize Script

I have a script that will loop through the rows of a specific column in my Google Sheet and then format the entire row based on the value that is contained within the cell. 我有一个脚本,它将遍历我的Google表格中特定列的行,然后根据单元格中包含的值来格式化整个行。 The problem is that this script is very slow because I am using getValue on each individual row of the column range, rather than using getValues on the entire column and referencing it like an array. 问题在于此脚本非常慢,因为我在列范围的每一行上都使用了getValue,而不是在整个列上都使用了getValues,并且像数组一样对其进行引用。

See the original script below: 请参阅下面的原始脚本:

function rowLoop() {
  var ss = SpreadsheetApp.openById("Fake_ID");
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Fake Name"));
  var endRow = sheet.getLastRow();
  // <= to repeat for all rows
  for (var r = 1; r <= endRow; r++) {
    rowAlignment(r);
    }
}

function rowAlignment(r) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var c = sheet.getLastColumn();
  var row = sheet.getRange(r, 2, 1, c);
  // Get cell in column E of row
  var typeCell = row.getCell(1,25);
  // Get its value
  var typeData = typeCell.getValue();
  // Test equal to 'Post' with ==
  if(typeData == 'Post') {
     row.setHorizontalAlignment('right').setFontSize('6').setFontStyle('italic').setFontWeight('normal');
  }
  else if (typeData == 'Campaign') {
     row.setFontWeight('bold').setHorizontalAlignment('left').setFontSize('8').setFontStyle('normal');
  }
  SpreadsheetApp.flush();
}

The script does exactly what it's meant to, but it's just slow. 该脚本完全可以达到其目的,但是速度很慢。 I tried optimizing it by using getValues rather than getValue. 我尝试使用getValues而不是getValue对其进行优化。 This is what I've written so far, but the issue is that the script doesn't do anything. 到目前为止,这就是我写的内容,但是问题是该脚本无法执行任何操作。 It doesn't pop any errors, it just doesn't seem to do anything. 它不会弹出任何错误,只是似乎什么也没做。 See below: 见下文:

function rowTestLoop() {
  var ss = SpreadsheetApp.openById("Fake_ID");
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Fake_Name"));
  var endRow = sheet.getLastRow();
  var endCol = sheet.getLastColumn();
  var data = sheet.getRange(1,1, endRow, endCol).getValues();
  // <= to repeat for all rows
  for (var r = 1; r <= endRow; r++) {
    var currentRow = sheet.getRange(r, 2, 1, endCol);
    if(data[r][24] == 'Post') {
      currentRow.setHorizontalAlignment('right').setFontSize('6').setFontStyle('italic').setFontWeight('normal');
    }
    else if (data[r][24] == 'Campaign') {
     currentRow.setHorizontalAlignment('left').setFontSize('8').setFontStyle('normal').setFontWeight('bold');
    }
  }
  SpreadsheetApp.flush();
}

Can anyone please help? 谁能帮忙吗?

So I believe I have figured it out. 所以我相信我已经弄清楚了。

Your are trying to look for tags post and Campaign in column number 24. However when you extract the sheet data into a array you have account for that fact that array indices start at '0' where as spreadsheet indices start at '1' 您正在尝试在第24列中查找标签post和Campaign。但是,当您将工作表数据提取到数组中时,您要考虑到数组索引从“ 0”开始,而电子表格索引从“ 1”开始的事实

Orginal Code: if(data[r][24] == 'Post') 原始代码:if(data [r] [24] =='Post')

Edited Code: if(data[r-1][23] == 'Post') 编辑代码:if(data [r-1] [23] =='Post')

So basically you need to modify indices to match that of the array and not the spreadsheet. 因此,基本上,您需要修改索引以匹配数组而不是电子表格的索引。 In other words Row 1 in spreadsheet is element 0 in an array, similarly Row 1, column 12 in the spreadsheet is element[0][11] in an array 换句话说,电子表格中的第1行是数组中的元素0,类似地,电子表格中的第1行,第12列是数组中的element [0] [11]

function rowTestLoop() {
  var ss = SpreadsheetApp.openById("Fake_ID");
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Fake_Name"));
  var endRow = sheet.getLastRow();
  var endCol = sheet.getLastColumn();
  var data = sheet.getRange(1,1, endRow, endCol).getValues();
  // <= to repeat for all rows
  for (var r = 1; r <= endRow; r++) {
    var currentRow = sheet.getRange(r, 2, 1, endCol);
    if(data[r-1][23] == 'Post') {
      currentRow.setHorizontalAlignment('right').setFontSize('6').setFontStyle('italic').setFontWeight('normal');
    }
    else if (data[r-1][23] == 'Campaign') {
     currentRow.setHorizontalAlignment('left').setFontSize('8').setFontStyle('normal').setFontWeight('bold');
    }
  }
  SpreadsheetApp.flush();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格脚本根据列单元格中的值锁定或解锁行 - Google Sheets Script to lock or unlock a row depending on the value in the cell of a column 编辑谷歌表格应用脚本中过滤行中的单元格值 - Edit cell value from filtered row in app script for google sheets 在Google表格脚本中设置活动单元格的值 - Set value of active cell in Google sheets script Google表格脚本查找具有价值的单元格 - Google Sheets Script Find Cell with Value 根据单元格值滚动一行到屏幕顶部:Google Sheet Script - Scroll a row to the top of the screen based on cell value: Google Sheet Script 如何编写Google Sheets脚本函数以根据同一行中的单元格识别值 - How to write a Google Sheets script function that identifies value depending on cell in the same row 使用 Google 表格的 Apps 脚本,在单元格值更改后插入 Header 行 - Using Apps Script for Google Sheets, Insert a Header Row after change in cell value 如果单元格以特定文本开头,则删除行 - Google Sheets / Apps Script - Delete Row if cell starts with certain text - Google Sheets / Apps Script 谷歌应用程序脚本返回单元格中的行值 - google apps script return row value in a cell Google表格:根据单元格值将一行数据移动到另一张表格 - Google Sheets: Move a row of data to another sheet based on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM