Google表格脚本用于插入新行并在单元格不匹配时复制某些值

时间:2018-03-19 03:32:45

标签: javascript google-apps-script google-sheets

我是JavaScript的新手,我正在尝试创建一个脚本,如果两个单元格不匹配,将插入一个新行并插入某些值。

此时我不知道下一个转弯的地方。我们的想法是,对于每个工单,最后添加项目经理的一行。我的问题,我相信行插入的位置和两个值的评估。行最终插入数据上方或每行之间。谢谢你的帮助。

function karpify() {
  onOpen();
  sort();
  checkInsert();
}

/** Build a menu item
From https://developers.google.com/apps-script/guides/menus#menus_for_add-ons_in_google_docs_or_sheets 
**/

function onOpen(e) { 
  var menu = SpreadsheetApp.getUi().createMenu('Transform'); 
  if (e && e.authMode == ScriptApp.AuthMode.NONE) {
    // Add a normal menu item (works in all authorization modes).
    menu.addItem('Insert PM Rows', 'karpify');
  } else {
    // Add a menu item based on properties (doesn't work in AuthMode.NONE).
    var properties = PropertiesService.getDocumentProperties();
    var workflowStarted = properties.getProperty('workflowStarted');
    if (workflowStarted) {
      menu.addItem('Insert PM Rows', 'karpify');
    } else {
      menu.addItem('Insert PM Rows', 'karpify');
    }
    menu.addToUi();
  }
}

function sort() {

  // Global vars
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var values = sheet.getDataRange().getValues();
  var employeeIndex = values[0].indexOf("Employee");
  var employeeCol = (employeeIndex++);
  var woCol = (values[0].indexOf("Work Order")+1);

  /**  Variables for customization:

  Each column to sort takes two variables: 
  1) the column index (i.e. column A has a column index of 1
  2) Sort Asecnding -- default is to sort ascending. Set to false to sort descending **/

  //Variable for column to sort first  
  var sortFirst = woCol; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
  var sortFirstAsc = true; //Set to false to sort descending

  //Variables for column to sort second
  var sortSecond = employeeCol;
  var sortSecondAsc = true;

  //Number of header rows  
  var headerRows = 1; 

  /** End Variables for customization**/

  /** Begin sorting function **/
  var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn());
  range.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}]);
}

function checkInsert() {

  // vars for check 
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheets()[0];
  var values = sheet.getDataRange().getValues();
  var employeeIndex = values[0].indexOf("Employee");
  var employeeCol = (employeeIndex + 1);
  var woIndex = values[0].indexOf("Work Order");
  var woCol = woIndex+1;
  var jcIndex = values[0].indexOf("Job Code");
  var jcCol = parseInt(jcIndex+1);
  var hoursCol = (values[0].indexOf("Regular Hours")+1);
  var rateCol = (values[0].indexOf("Regular Rate")+1);
  var columns = sheet.getLastColumn();
  var rows = sheet.getMaxRows();
  //vars for insert
  for (var row = 2; row <= rows; row++) {
    var insert = sheet.insertRowAfter(row);
    var test = values[row][woCol];
    var test2 = values[row++][woCol];
    var range = sheet.getRange(row,1,1); // store the range to copy 
    if (test !== test2) {
      sheet.insertRowAfter(row);
      var newRange = sheet.getRange(row+1, sheet.getMaxColumns(), 1); // store the location of the new row
      range.copyTo(newRange); // copy saved row data to the new row
      sheet.getRange(row+1,rateCol).setValue(27);
      sheet.getRange(row+1,employeeCol).setValue("Smith, Bill");
      sheet.getRange(row+1,hoursCol).setValue(.5);
      var rows = sheet.getLastRow();
    }
  }
}

0 个答案:

没有答案