新数据集条目与仅选择新数据的当前数据集进行比较

时间:2019-07-10 16:37:45

标签: google-apps-script google-sheets comparison google-sheets-formula

Spreadsheet-忽略Macros.gs。

我为这项任务的效率而苦苦挣扎,因为我相信有比我更简单的方法。

我正在尝试使用“ towquest”表中的数据填充该表,其中我实际上只需要四列:日期,时间,ID,组,其中F列=136。此表表示预订列表。我没有直接访问我们的数据库的权限,因此只能取出此Excel文件。此列表仅包含尚未签到的那些预订。

想象一下,您在早上提取数据,然后在下午提取数据,那一天以后已经签到的客户会更短一些,而有些已经预订的客户会更短。

数据一旦填充完毕,我需要更新此工作表,例如在晚上或其他早晨,我需要将以前的数据与新的数据进行比较,以避免重复。

因此,我为此创建了三个特殊的工作表:DataBlackBox / database /,查询和唯一工作表。

添加新数据的方法:

COPY DATARANGE FROM UNIQUE TO DATABLACKBOX to ARTIFICIALLY CREATE DUPLICATES

SELECT ONLY DATA WITH BRANCH NUMBER 136, FORMAT TIME AND DATE AND COPY to DATABLACKBOX

IN QUERY SHEET ARE SELECTED ONLY DATA APPEARING ONCE = VERY NEW BOOKINGS

COPY DATA FROM QUERY SHEET AND ADD ONTO DASHBOARD

DELETE DATABLACKBOX SHEET DATA AND POPULATE THEM WITH UNIQUES ONLY / this is done to avoid thousands and thousands of rows and speed up the process /

仅在手动上添加1个新预订,跳过第2步,就可以完成相同的过程

我的问题是,我是否可以更好地完成整个过程-就像不使用三张纸一样。我期望比这更简单的代码。它可以工作,但是我很乐意看到专业人士提供更好的解决方案,甚至提示。

function searchSheet() {  //search sheet by keyword = towquest - the new entry data sheet always contains towquest
  var out = new Array();
  var key = "towquest"; // keyword
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); //get name of sheets into an array
  // loop runs through the array to find the keyword
  for ( var i = 0; i<sheets.length; i++){
    out[i] = sheets[i].getName();
    if( out[i].indexOf(key) > -1 ){ // indexOf() gives positive number when keyword found
      var result = out[i];
      break;
    }   
  }
  Logger.log(result);
  return result; 
}


function rawDataFormatFilter() { // time and date cells are not in the right format
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sheet.getSheetByName(searchSheet());
  var lastRow = ss.getLastRow();
  ss.insertColumnBefore(2);
  var downrange = ss.getRange(2, 2, lastRow-1);
  ss.getRange("B2").setFormula("=DATEVALUE(C2)").copyTo(downrange);
  var dateFormat = "dd/mm";
  var range = ss.getRange("B2:B");
  range.setNumberFormat(dateFormat);
  ss.insertColumnBefore(4);
  var downrange2 = ss.getRange(2, 4, lastRow-1);
  ss.getRange("D2").setFormula("=TIME(E2/100,MOD(E2,100),)").copyTo(downrange2);
  var timeFormat = "HH:mm";
  var range2 = ss.getRange("D2:D");
  range2.setNumberFormat(timeFormat);

  var queryRange = ss.getRange("$T$1").setValue("=QUERY(B:S, \"Select B,D,F,N,S where H=136\",1)"); // create query
  var downrange3 = ss.getRange("S2:S");

  var timezone = "GMT+1";
  var timestamp_format = "dd/MM/yy HH:mm"; // Timestamp Format. 
  var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
  ss.getRange("$S$2").setValue(date).copyTo(downrange3);

  var copyRange = ss.getRange("T2:X");
    Logger.log(copyRange);
  return copyRange;

};

function uniqueDataPaste(paste){ //copypaste data from unique sheet

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var uniqueSheet = sheet.getSheetByName("UNIQUE");
  var uniqueRange = uniqueSheet.getRange(1, 1, uniqueSheet.getLastRow(), uniqueSheet.getLastColumn());
  uniqueRange.copyTo(paste, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  Logger.log(uniqueRange);
}

function rawDataPaste() { //copypaste data from new dataset sheet

  var sheet = SpreadsheetApp.getActiveSpreadsheet(); 
  var dataSetSheet = sheet.getSheetByName(searchSheet());
  var pasteSheet = sheet.getSheetByName('DataBlackBox');
  var lastRow = sheet.getLastRow(); 
  var paste = pasteSheet.getRange(pasteSheet.getLastRow()+1, 1);
  Logger.log(paste);

  uniqueDataPaste(paste);
  dataSetSheet.getRange("T2:X").copyTo(paste, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

  //dataSetSheet.activate();
  //SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();
};

function bookingPaste() { //copypaste AddBooking sheet data

  var sheet = SpreadsheetApp.getActiveSpreadsheet(); 
  var addSheet = sheet.getSheetByName('AddBooking');
  var pasteSheet = sheet.getSheetByName('DataBlackBox');
  var lastRow = pasteSheet.getLastRow(); 
  var paste = pasteSheet.getRange(pasteSheet.getLastRow()+1, 1);
  Logger.log(paste);

  uniqueDataPaste(paste);
  addSheet.getRange("B5:E20").copyTo(paste, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};

function sortDash(){ // sorts dash by date
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('DASHBOARD');
  sheet.getRange("B5:R");
  sheet.sort(2, true);
}

function insertIntoDash(){ // inserts data into dashboard sheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var querySheet = sheet.getSheetByName("QUERY");
  var dashSheet = sheet.getSheetByName("DASHBOARD");
  var queryRange = querySheet.getRange(1, 1, querySheet.getLastRow(), querySheet.getLastColumn());
  queryRange.copyTo(dashSheet.getRange(dashSheet.getLastRow()+1, 2), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  Logger.log(queryRange);
  //inserts data to DASHBOARD only from QUERY sheet + insert checkboxes
  sortDash();
}

function blackboxClearOut(){ //clears out data from DataBlackBox to avoid thousands of rows by updating
 var sheet = SpreadsheetApp.getActiveSpreadsheet();
 var blackboxSheet = sheet.getSheetByName('DataBlackBox');
 var uniqueSheet = sheet.getSheetByName('UNIQUE');
 var uniqueCopy = uniqueSheet.getRange(1, 1, uniqueSheet.getLastRow(), uniqueSheet.getLastColumn()-1); // -1 cause there's timeStamp, not in use for now

 uniqueCopy.copyTo(uniqueSheet.getRange(1, uniqueSheet.getLastColumn()+1), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false) 
 var uniqueStorageRange = uniqueSheet.getRange(1, 6, uniqueSheet.getLastRow(),uniqueSheet.getLastColumn());
 blackboxSheet.getRange(1, 1, blackboxSheet.getLastRow(), blackboxSheet.getLastColumn()-2).clearContent(); // -2 cause there's text now 
 uniqueStorageRange.copyTo(blackboxSheet.getRange(1, 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
 uniqueStorageRange.clear();
}



function towquestPaste(){ 
  var result;
  searchSheet();
  rawDataFormatFilter(result);
  rawDataPaste();
  insertIntoDash();
  blackboxClearOut();
}

function addBookingPaste(){
  bookingPaste();
  insertIntoDash();
  blackboxClearOut();
}

1 个答案:

答案 0 :(得分:0)

在没有详细介绍代码的情况下,仅提供一些一般提示:

  1. Apps脚本具有可与dates一起使用的指定功能
  2. 直接在Apps脚本中执行简单的计算,而不是设置公式
  3. 考虑通过实现循环和条件语句(与&&或||互连)而不是过滤器来选择感兴趣的范围/行,这将节省您的过滤和复制步骤。

花一些时间研究Apps脚本reference documentation,并进一步了解Apps Script的思维方式和编码方式,而不是手动完成工作表中的内容。