删除列a

时间:2019-02-05 13:40:02

标签: javascript google-sheets

enter image description here仅当列“ A”不为空白时,脚本才根据列“ T”中的指定值在行的非空白单元格中添加单元格背景色。我遇到的问题是脚本没有检查“ A”列,然后如果“ A”列中没有值,则删除行的背景色。我正在寻找是否有人可以告诉我我要去哪里,在脚本的底部是我检查“ A”列,然后将背景设置为“无”的地方。无论如何,有没有优化此脚本的程序?它需要一段时间才能运行。

function colors() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet_name = ['Current Sales','Pending Orders', 'Subcontract Orders', 'Partial Shipped', 'Quotes', 'Archived Sales']; 
  for (s=0; s<sheet_name.length; s++) {
    var sheet = ss.getSheetByName(sheet_name[s]);
    var range = sheet.getDataRange()
    var values = range.getValues();
            
     for (var i = 0; i < values.length; i++) {
     for (var j = 0; j < values[i].length; j++) {
     if (values[i][j] !== "" && values[i][19] == "Complete") {
        range.getCell(i + 1, j + 1).setBackground("#99FF99");          
        }
   
       if (values[i][j] !== "" && values[i][19] == "Revise") {
        range.getCell(i + 1, j + 1).setBackground("#FA5959");
       }
 
       if (values[i][j] !== "" && values[i][19] == "Pending Next Step") {
        range.getCell(i + 1, j + 1).setBackground("#FFFF33");
       }
 
       if (values[i][j] !== "" && values[i][19] == "Quoted") {
        range.getCell(i + 1, j + 1).setBackground("#FFCCCC");
       }

       if (values[i][j] !== "" && values[i][19] == "Awaiting Vendor Response") {
        range.getCell(i + 1, j + 1).setBackground("#FFCC99");
       }

       if (values[i][j] !== "" && values[i][19] == "Awaiting Customer Response") {
        range.getCell(i + 1, j + 1).setBackground("#FF9999");
       }

       if (values[i][j] !== "" && values[i][19] == "Parts On Order") {
        range.getCell(i + 1, j + 1).setBackground("#FF6666");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Shipped Partial Order") {
        range.getCell(i + 1, j + 1).setBackground("#FFB266");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Awaiting SubContract Parts Work") {
        range.getCell(i + 1, j + 1).setBackground("#66FFFF");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Picking List Printed") {
        range.getCell(i + 1, j + 1).setBackground("#FF9333");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Waiting on BOL / Charges") {
        range.getCell(i + 1, j + 1).setBackground("#3399FF");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Quote Sold") {
        range.getCell(i + 1, j + 1).setBackground("#FF66B2");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Awaiting Vendor Confirmation") {
        range.getCell(i + 1, j + 1).setBackground("#CC99FF");
       }
       
       if (values[i][j] !== "" && values[i][19] == "Ready To Ship") {
        range.getCell(i + 1, j + 1).setBackground("#FFE5CC");
       }

       if (values [i][j] == "" && values[i][0] == "") {
         range.getCell(i + 1, j + 1).setBackground('none');
     }
    }
   }
  }
 }

function colors() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet_name = ['Current Sales','Pending Orders', 'Subcontract Orders', 'Partial Shipped', 'Quotes', 'Archived Sales']; 
  for (s=0; s<sheet_name.length; s++) {
    var sheet = ss.getSheetByName(sheet_name[s]);
    var range = sheet.getRange(2,1,sheet.getMaxRows(),sheet.getMaxColumns());
    var values = range.getValues();
    var cIndex;
    var TArr = ['Complete','Revise','Pending Next Step','Quoted','Awaiting Vendor Response','Awaiting Customer Response','Parts On Order','Shipped Partial Order','Awaiting SubContract Parts Work','Picking List Printed','Waiting on BOL / Charges','Quote Sold','Awaiting Vendor Confirmation','Ready To Ship'];
    var CArr = ['#99FF99','#FA5959','#FFFF33','#FFCCCC','#FFCC99','#FF9999','#FF6666','#FFB266','#66FFFF','#FF9333','#3399FF','#FF66B2','#CC99FF','#FFE5CC']
            
     for (var i = 0; i < values.length; i++) {
  if (values[i][0] !== "") {  // only if A is not empty
    for (var j = 0; j < values[i].length; j++) {
      if (values[i][j] !== "") {
        cIndex = TArr.indexOf(values[i][19]);
        if (cIndex != -1) {
          range.getCell(i + 1, j + 1).setBackground(CArr[cIndex]);
        }
      }
    } 
  }
}
}
}

2 个答案:

答案 0 :(得分:1)

如果您有一个数组,该数组的T值(TArr)和另一个具有相同颜色(CArr)的顺序相同,则可以这样设置颜色:在TArr中找到T值的索引并设置颜色如果A不为空,则返回CArr [index],否则为'none'。

只需对T单元格进行一次检查即可找到相应的颜色。

ax

答案 1 :(得分:0)

我在修复代码方面获得了帮助。这是大大减少了执行时间的工作脚本。 @Olafant,非常感谢您的协助。

function SetFormatting() {
  SpreadsheetApp.getUi()
  .createMenu('Apply Borders/Colors')
  .addItem('Set Borders','bordersNoArg')
  .addSeparator()
  .addSubMenu(SpreadsheetApp.getUi().createMenu('Apply Colors')
              .addItem('Apply Colors', 'colorsNoArg'))              
  .addToUi();
}

function borders(sheet_name) {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  for (s=0; s<sheet_name.length; s++) {
    var sheet = ss.getSheetByName(sheet_name[s]);    
    var range = sheet.getRange(2,1,sheet.getLastRow(),sheet.getLastColumn());
    var values = range.getValues();
    var offsetRow = range.getRowIndex();
                  
    range.setBorder(false, false, false, false, false, false);
    SpreadsheetApp.flush();
    for (var i = 0; i < values.length; i++) {
      if (values[i][0]) { // Only for non-empty A column cells
        sheet.getRange("A" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, false, false, "black",SpreadsheetApp.BorderStyle.DOUBLE);
        for (var j = 0; j < values[i].length; j++) {
          if (values[i][j] !== "") {
            range.getCell(i + 1, j + 1).setBorder(null,true,null,true,true,true);
          }
        }
      } 
    }
  }    
}

function bordersNoArg(){
  var sheet_name = ['Current Sales','Pending Orders', 'Subcontract Orders', 'Partial Shipped', 'Quotes', 'Archived Sales'];
  borders(sheet_name)
}

function colors(sheet_name) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  for (s=0; s<sheet_name.length; s++) {
    var sheet = ss.getSheetByName(sheet_name[s]);
    var range = sheet.getRange(2,1,sheet.getLastRow(),sheet.getLastColumn());
    var values = range.getValues();
    var cIndex;
    var TArr = ['Complete','Revise','Pending Next Step','Quoted','Awaiting Vendor Response','Awaiting Customer Response','Parts On Order','Shipped Partial Order','Awaiting SubContract Parts Work','Picking List Printed','Waiting on BOL / Charges','Quote Sold','Awaiting Vendor Confirmation','Ready To Ship'];
    var CArr = ['#99ff99','#fa5959','#ffff33','#ffcccc','#ffcc99','#ff9999','#ff6666','#ffb266','#66ffff','#ff9333','#3399ff','#ff66b2','#cc99ff','#ffe5cc']
    var exisBackGrounds = range.getBackgrounds();
    
    //Set Empty rows background to null
    var emptyRng = sheet.getRange((sheet.getLastRow() + 2),1,sheet.getMaxRows(),sheet.getMaxColumns());
    emptyRng.setBackground(null);
    
    for (var i = 0; i < values.length; i++) {
      if (values[i][0] !== "") { // only if A is not empty
        for (var j = 0; j < values[i].length; j++) {
          if (values[i][j] !== "") {
            cIndex = TArr.indexOf(values[i][19]);
            if (cIndex != -1) {
              if(exisBackGrounds[i][j] != CArr[cIndex]){
                range.getCell(parseInt(i) + 1, parseInt(j) + 1).setBackground(CArr[cIndex]);
              }
            }
          }else{
            if(exisBackGrounds[i][j] != '#ffffff'){
                range.getCell(parseInt(i) + 1, parseInt(j) + 1).setBackground(null);
              } 
          }
        }
      }else{
        sheet.getRange(parseInt(i) + 2,1,1,sheet.getLastColumn()).setBackground(null);
      }
    }
  }
}


function colorsNoArg() {
  var sheet_name = ['Current Sales','Pending Orders', 'Subcontract Orders', 'Partial Shipped', 'Quotes', 'Archived Sales'];
  colors(sheet_name);
}