根据单元格值更改行对齐 - Google Scripts

时间:2016-12-17 16:31:38

标签: javascript google-sheets

以下脚本应该将Google表格中的行对齐方式更改为“正确”#39;如果行中的列E的值是“发布”。'当我运行脚本时,它不会弹出任何错误,但是,它也没有做任何事情。

任何人都可以看看下面的内容并提供一些指导吗?

谢谢!

function rowLoop() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("KOD Tests");
  var endRow = ss.getLastRow();

  for (var r = 1; r < endRow; r++) {
    rowAlignment(r);
    }
}

function rowAlignment(r) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var c = sheet.getLastColumn();
  var dataRange = sheet.getRange(r, 1, 1, c);

  var data = dataRange.getValue();
  var row = data[4];

  if(row === 'Post') {
    data.setHorizontalAlignment('right');
  }
  SpreadsheetApp.flush();
}

1 个答案:

答案 0 :(得分:0)

这里修复了一些问题(请参阅评论)

function rowLoop() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var endRow = ss.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, 1, 1, c);
  // Get cell in column E of row
  var cell=row.getCell(1,5);
  // Get its value
  var data = cell.getValue();
  // Test equal to 'Post' with ==
  if(data == 'Post') {
    row.setHorizontalAlignment('right');
  }
  SpreadsheetApp.flush();
}