1

我在谷歌文档电子表格中有三个脚本。在此电子表格的 H 列(或第 8 列)中,如果我键入“x”,脚本会将其更改为该日期。几天后,H 列中的每个日期都从一个日期变成了一个数字。这些数字如下所示:40492、40494、40511。我不确定是什么原因造成的。也许这是我的脚本有问题。我已经把它们贴在下面了。有任何想法吗?

function onEdit(e) {

  var colorA = "yellow";
  var colorB = "#dddddd";
  var colorC = "#dddddd";
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Purchase Orders");
  var range = e.source.getActiveRange();
  var sheetName = SpreadsheetApp.getActiveSheet().getName();

  if (sheetName == "Purchase Orders") {

 // 3 is column C 
    if (range.getColumn() == 3 && range.getValue() != "") {
      sheet.insertRowAfter(range.getRow());
      var r = range.getRow() + 1;
      sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
    }
  }

    var col = e.source.getActiveRange().getColumn();
    if(col == 3 || col == 8) {
    var rows = sheet.getMaxRows();

    //column C
    var rangeC = sheet.getRange("C1:C"+rows);
    var valuesC = rangeC.getValues();
    //column H range
    var rangeH = sheet.getRange("H1:H"+rows);
    var colorH = rangeH.getBackgroundColors();
    var valuesH = rangeH.getValues();

    //iterate over each row in column C and H
    //then change color
        for (var row = 0; row < valuesC.length; row++) {
          //check for columnC and column H
          var hRow = colorH[row];
          if (valuesC[row][0] != "" && valuesH[row][0] == "") {
            hRow[0] = colorA;
          } else if (valuesH[row][0] != "") {
            hRow[0] = colorB;
      }
        }
    sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);




}
  }

和这个

function onEdit(e) {
  var ss = e.source.getActiveSheet();
  var r = e.source.getActiveRange();
  // 1 is A, 2 is B, ... 8 is H
  if (r.getColumn() == 8 && r.getValue() == "x") {
    r.setValue(Utilities.formatDate(new Date(), "MST", "yyyy-MM-dd"));

  }  
}  

最后一个

ss = SpreadsheetApp.getActiveSpreadsheet();


function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
   ss.addMenu("New PO", menuEntries);
 }


function NewPO() {
  SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);


  // Adjust this range accordingly, these are the cells that will be
  // copied.  Format is getRange(startRow, startCol, numRows, numCols)
  ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
      .copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
   }
4

1 回答 1

3

在 OnEdit 中,您可能还想设置该单元格的格式。setNumberFormat(numberFormat) 似乎是您所追求的功能。

http://code.google.com/googleapps/appsscript/class_range.html#setNumberFormat

于 2010-12-12T01:24:53.320 回答