为什么在 Google Sheets App Script 中批量操作 "setFontWeights(Array)" 功能不起作用?

Why is batch operating in Google Sheets App Script such as "setFontWeights(Array)" function not working?

我发现我的 Google 应用程序脚本存在问题。使用 setFontWeights(Array) 函数无效。我对 setValues(Array) 函数做了另一个测试,它也没有用。

我想做的事情:

  1. 选择“是”选项开始设置字体粗细。
  2. 程序将检查“SCORE”列,如果“SCORE”为空(“”),则该行的字体粗细设置为“粗体”,否则,字体粗细设置为“正常” “

问题: 测试错误结果,COMMENT line 46 to line 50 and UNCOMMENT line 53。 第 46 行到第 50 行的代码是我当前的解决方案,因为第 53 行不起作用。

以下是Google应用程序脚本的副本:

let app = SpreadsheetApp;
let ss = app.getActiveSpreadsheet();
let activeSheet = ss.getActiveSheet();
let sheetName = activeSheet.getName();

let sheet1 = ss.getSheetByName("Sheet1");

let loopOptionRange = sheet1.getRange(4, 4);
let loopOption = loopOptionRange.getValue();
let loopStatusRange = sheet1.getRange(4, 7);

function onEdit(e) {
}

function onOpen() {
  loopOptionRange.setValue("Choose");
  sheet1.getRange(4, 5).setValue(5);
  loopStatusRange.setValue("");
}

function loopTest() {
  const startRow = 4; //table head
  const lastRow = sheet1.getLastRow();
  sheet1.getRange(4, 6).setValue(lastRow);
  const startRowLoop = startRow + 1; //first row of looping

  try {

    let textFormats = new Array(500);
    for (x = startRowLoop; x <= lastRow; x++) {
      const testStatus = sheet1.getRange(x, 3).getValue();
      textFormats[x] = new Array(3);
     
      if (testStatus == "") {
        for (y = 1; y <= 3; y++) {
          textFormats[x][y] = "bold";
        }
      } else {
        for (y = 1; y <= 3; y++) {
          textFormats[x][y] = "normal";
        }
      }
    }

    //LINE 46 to 50 WORKS
    for (x = startRowLoop; x <= lastRow; x++) {                   //LINE 46
      for (y = 1; y <= 3; y++) {                                  //LINE 47
        sheet1.getRange(x, y).setFontWeight(textFormats[x][y]);   //LINE 48
      }                                                           //LINE 49
    }                                                             //LINE 50

    //LINE 53 DOES NOT WORK
    // sheet1.getRange(startRowLoop, 1, lastRow - startRowLoop + 1, 3).setFontWeights(textFormats); //LINE 53

    loopStatusRange.setValue("Done");
    loopOptionRange.setValue("Choose");

  } catch (error) {
    app.getUi().alert(`An error occurred.`);
  }
}

if (sheetName === "Sheet1"){
  if (loopOption == "Yes") {
    loopStatusRange.setValue("Processing ...");
    loopTest();
  } else if (loopOption === "Cancel") {
      loopOptionRange.setValue("Choose");
  }
}

BATCH OPERATING - ARRAY - Google Sheets File

如果你这样重新组织它似乎 运行 可以,但我只复制了 Sheet1

function onEdit(e) {
}
function onOpen() {
  loopOptionRange.setValue("Choose");
  sheet1.getRange(4, 5).setValue(5);
  loopStatusRange.setValue("");
}
//I ran this function with no errors
function myfunk() {
  let ss = SpreadsheetApp.getActive();
  let activeSheet = ss.getActiveSheet();
  let sheetName = activeSheet.getName();
  let sheet1 = ss.getSheetByName("Sheet1");
  let loopOptionRange = sheet1.getRange(4, 4);
  let loopOption = loopOptionRange.getValue();
  let loopStatusRange = sheet1.getRange(4, 7);
  if (sheetName === "Sheet1") {
    if (loopOption == "Yes") {
      loopStatusRange.setValue("Processing ...");
      loopTest();
    } else if (loopOption === "Cancel") {
      loopOptionRange.setValue("Choose");
    }
  }
}
function loopTest() {
  const startRow = 4; //table head
  const lastRow = sheet1.getLastRow();
  sheet1.getRange(4, 6).setValue(lastRow);
  const startRowLoop = startRow + 1; //first row of looping
  try {
    let textFormats = new Array(500);
    for (x = startRowLoop; x <= lastRow; x++) {
      const testStatus = sheet1.getRange(x, 3).getValue();
      textFormats[x] = new Array(3);
      if (testStatus == "") {
        for (y = 1; y <= 3; y++) {
          textFormats[x][y] = "bold";
        }
      } else {
        for (y = 1; y <= 3; y++) {
          textFormats[x][y] = "normal";
        }
      }
    }
    for (x = startRowLoop; x <= lastRow; x++) {                   //LINE 46
      for (y = 1; y <= 3; y++) {                                  //LINE 47
        sheet1.getRange(x, y).setFontWeight(textFormats[x][y]);   //LINE 48
      }                                                           //LINE 49
    }                                                             //LINE 50
    sheet1.getRange(startRowLoop, 1, lastRow - startRowLoop + 1, 3).setFontWeights(textFormats); //LINE 53
    loopStatusRange.setValue("Done");
    loopOptionRange.setValue("Choose");
  } catch (error) {
    app.getUi().alert(`An error occurred.`);
  }
}

在您的脚本中,let textFormats = new Array(500); 被声明,并且您在 for (x = startRowLoop; x <= lastRow; x++) {,,,} 的循环中使用此数组。在这种情况下,前 5 个元素为空。并且,textFormats 的每个元素都是在 for (y = 1; y <= 3; y++) {,,,} 的循环中创建的。在这种情况下,每个元素有 4 个元素。我认为在这种情况下,错误发生在sheet1.getRange(startRowLoop, 1, lastRow - startRowLoop + 1, 3).setFontWeights(textFormats);。我认为这些是您遇到问题的原因。

当这些点反映到你的脚本中,就变成了下面这样。

发件人:

let textFormats = new Array(500);
for (x = startRowLoop; x <= lastRow; x++) {
  const testStatus = sheet1.getRange(x, 3).getValue();
  textFormats[x] = new Array(3);
 
  if (testStatus == "") {
    for (y = 1; y <= 3; y++) {
      textFormats[x][y] = "bold";
    }
  } else {
    for (y = 1; y <= 3; y++) {
      textFormats[x][y] = "normal";
    }
  }
}

收件人:

var values = sheet1.getRange(startRowLoop, 1, lastRow - 4, 3).getValues();
var textFormats = values.map(([a,b,c]) => c == "" ? Array(3).fill("bold") : Array(3).fill("normal"));
  • 在这种情况下,请使用 sheet1.getRange(startRowLoop, 1, lastRow - startRowLoop + 1, 3).setFontWeights(textFormats); 而不是 for (x = startRowLoop; x <= lastRow; x++) {,,,}

参考文献: