Google Script复制并粘贴为值调试

时间:2020-08-28 05:38:10

标签: date google-apps-script copy-paste

希望

  1. 在第一行中查找与昨天的日期匹配的单元格(此部分有效)
  2. 选择单元格所属的列
  3. 复制整个列并粘贴为值

不确定代码在哪里中断

function CopyandPasteasValues() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[1]; 
  // sheet is the first worksheet in the spreadsheet

  var today = new Date();
  var yesterday = new Date();
  yesterday.setDate(today.getDate()-1);
  yesterday.setHours(3,0,0,0); //comparison doesn't seem to work without this

  for(var i=0; i< 26; i++) {
     
    var cell = sheet.getRange(1,i+2); //start getting sheet date at cell C1
    var sheetdate = cell.getValue();

    if(sheetdate.valueOf()  == yesterday.valueOf()) {

      // if there's a match, set the col
      var col = (i);
      
      // copy column and paste as values -- BELOW DOESN"T WORK --
      function copyandpastescol() {
        sheet.getRange(1, col+2, sheet.getMaxRows(), 1).activate();
        sheet.getActiveRange().copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

Duh ...不需要额外的功能!谢谢...

/** @OnlyCurrentDoc */

function CopyandPasteasValues() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // ss is now the spreadsheet the script is associated with
  var sheet = ss.getSheets()[1]; // sheets are counted starting from 0
  // sheet is the first worksheet in the spreadsheet

  
  // set and store a date object for today
  var today = new Date();
  var yesterday = new Date();
  yesterday.setDate(today.getDate()-1);
  yesterday.setHours(3,0,0,0);

  // iterate the values in the range object
  for(var i=0; i< 26; i++) {
     
    var cell = sheet.getRange(1,i+2); //start getting sheet date at cell C1
    var sheetdate = cell.getValue();

    // Compare only values of the objects
    if(sheetdate.valueOf()  == yesterday.valueOf()) {

      // if there's a match, set the col
      var col = (i+2);
      
      // copy column and paste as values
      sheet.getRange(1, col, sheet.getMaxRows(), 1).activate();
      sheet.getActiveRange().copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
     
    }
  }
}
相关问题