在google工作表中移动前检查该行

时间:2017-10-29 12:27:50

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

当第21列中的值=已发送

时,我使用此脚本将行移至另一张表

我需要在移动行检查之前是否在第6,15,18,19列中有空的特定单元格 如果有一些空单元格显示缺少值的警告

我不是一个程序员我试图理解脚本如何工作并将代码嵌入到其他代码中

所以,如果你可以帮我改写这个scrtip,以便更快,更好地工作

提前致谢

function onEdit() {

 var sheetNameToWatch1 = "Assign_Page";
 var columnNumberToWatch = 21;
 var valueToWatch = "Delivred";
 var sheetNameToMoveTheRowTo = "C_Delivery_Archive";
 var sheetNameToMoveTheRowTo2 = "Buffering";

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getActiveCell();

 if ( (sheet.getName() == sheetNameToWatch1) && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
 var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
 var targetSheet2 = ss.getSheetByName(sheetNameToMoveTheRowTo2);


 var targetRange2 = targetSheet2.getRange(targetSheet2.getLastRow()+ 1, 1);
 sheet.getRange(range.getRow(), 1, 1, 25).copyTo(targetRange2, {contentsOnly:true});

 var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
 sheet.getRange(range.getRow(), 1, 1, 25).copyTo(targetRange, {contentsOnly:true});

sheet.deleteRow(range.getRow());

var Sheet4 = ss.getSheetByName("Delivery_Information");
var Sheet1 = ss.getSheetByName("Dashbord");
var Sheet2 = ss.getSheetByName("Assign_Page");
var Sheet3 = ss.getSheetByName("Report");
var Sheet5 = ss.getSheetByName("Invoice_Info");
var Sheet6 = ss.getSheetByName("Fleet_Status");

 Sheet4.showSheet();
 Sheet1.hideSheet();
 Sheet2.hideSheet();
 Sheet3.hideSheet();
 Sheet5.hideSheet();
 Sheet6.hideSheet();
 ss.setActiveSheet(ss.getSheetByName('Delivery_Information'));     
 }
}

1 个答案:

答案 0 :(得分:0)

我认为这会做你想要的。如果第21列设置为“已交付”,则会检查第6,15,18和第19列以查看它们是否已填写。如果不是,则会弹出一个消息框,列出缺少的单元格。它还将'已交付'重置为空白。填写丢失的单元格并再次设置“已交付”。再次运行相同的检查。如果一切都在那里,则不会显示任何消息,并且脚本的其余部分会运行。

function onEdit() {
 var sheetNameToWatch1 = "Assign_Page";
 var columnNumberToWatch = 21;
 var sheetNameToMoveTheRowTo = "C_Delivery_Archive";
 var sheetNameToMoveTheRowTo2 = "Buffering";
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getActiveCell();
 var row= sheet.getActiveCell().getRow()
   if ( sheet.getName() == sheetNameToWatch1 && range.getColumn() == columnNumberToWatch && range.getValue() == "Delivered") {
   var cnt =checkCells()
   if(cnt > 0){return}//If all required cells are not filled in quit.
    else{
 var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
 var targetSheet2 = ss.getSheetByName(sheetNameToMoveTheRowTo2);


 var targetRange2 = targetSheet2.getRange(targetSheet2.getLastRow()+ 1, 1);
 sheet.getRange(range.getRow(), 1, 1, 25).copyTo(targetRange2, {contentsOnly:true});

 var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
 sheet.getRange(range.getRow(), 1, 1, 25).copyTo(targetRange, {contentsOnly:true});

sheet.deleteRow(range.getRow());

var Sheet4 = ss.getSheetByName("Delivery_Information");
var Sheet1 = ss.getSheetByName("Dashbord");
var Sheet2 = ss.getSheetByName("Assign_Page");
var Sheet3 = ss.getSheetByName("Report");
var Sheet5 = ss.getSheetByName("Invoice_Info");
var Sheet6 = ss.getSheetByName("Fleet_Status");

 Sheet4.showSheet();
 Sheet1.hideSheet();
 Sheet2.hideSheet();
 Sheet3.hideSheet();
 Sheet5.hideSheet();
 Sheet6.hideSheet();
 ss.setActiveSheet(ss.getSheetByName('Delivery_Information'));     
 }
  }}

function checkCells() {
  var ss=SpreadsheetApp.getActiveSpreadsheet()
  var s=ss.getActiveSheet()
  var row=ss.getActiveCell().getRow()
  var c=[6,15,18,19] //Array of columns to check
  var empty=[]//Array for empty cells
  for(i=0;i<c.length;i++){
    if(s.getRange(row,c[i]).getValue()==""){//If cell is blank
       var A1=s.getRange(row,c[i]).getA1Notation()//Get cell address
       empty.push(A1)//Write empty cell address to empty array
    }}
if(empty.length != 0){//If all required cell don't have a value
      Browser.msgBox("Cells "+empty+" can not be blank. Fill in then reset 'Delivered'", Browser.Buttons.OK_CANCEL) //Show message
      s.getRange(empty[0]).activate()//Activate first cell that needs a value
      s.getRange(row, 21).clearContent()//Set 'Delivered to blank.
  var rtn=empty.length //Return empty array count
  return rtn
  }}

如果要在单元格地址的消息instrad中写入第2行的标题,请将checkCells函数替换为:

function checkCells() {
  var ss=SpreadsheetApp.getActiveSpreadsheet()
  var s=ss.getActiveSheet()
  var row=s.getActiveCell().getRow()

  var c=[6,15,18,19] //Array of columns to check
  var empty=[]//Array for empty cells
  var header=[]//Array of Headers in row 2
  for(i=0;i<c.length;i++){
    if(s.getRange(row,c[i]).getValue()==""){//If cell is blank
       var A1=s.getRange(row,c[i]).getA1Notation()//Get cell address
       var hdr=s.getRange(2,c[i]).getValue()//Get the header values
       empty.push(A1)//Write empty cell address to empty array
       header.push(hdr)
    }}
  if(empty.length != 0){//If all required cell don't have a value
     // Browser.msgBox("Cells "+empty+" can not be blank. Fill in then reset 'Delivered'", Browser.Buttons.OK_CANCEL) //Show cell message
      Browser.msgBox("Column(s) "+header+" can not be blank. Fill in then reset 'Delivered'", Browser.Buttons.OK_CANCEL) //Show header message
      s.getRange(empty[0]).activate()//Activate first cell that needs a value
      s.getRange(row, 21).clearContent()//Set 'Delivered to blank.
  var rtn=empty.length //Return empty array count
  return rtn
  }}