【问题标题】:Suggestion for optimizing Script to "Paste Values Only" based on matching today's date根据匹配今天的日期优化脚本以“仅粘贴值”的建议
【发布时间】:2020-08-24 18:49:53
【问题描述】:

我希望有人能够就我在下面编写的这个脚本提供一些建议。我有一个包含大量工作表的电子表格,并希望根据与今天日期匹配的工作表内的日期“仅复制/粘贴值”到几列(目的是每天在特定时间运行脚本)。

我搜索了一些过去的主题以深入了解类似的格式类型,但似乎仍然无法使其正常运行;任何意见表示赞赏!

//Loops through individual sheets and checks to find current day's tab.  Once found it copy/paste specials the values only to lock the day's values.  
function lockDaysValues (){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var todaysDate = new Date(); 
    var todaysDateFormatted = Utilities.formatDate(new Date(),"PST","MM-dd-yyyy") //formats to fit 
    current tab name date titles of MM/DD/YYYY
    var sheets = ss.getSheets(); 
     for(var x=0; x<sheets.length; x++){
      checkSheets = sheets[x].getRange('C2').getValue();  
        if(checkSheets=='todaysDateFormatted'){
          ss.getActiveSheet().getRange('C5:D').activate();
          ss.getActiveSheet().getRange('C5:D').copyTo(ss.getActiveSheet().getRange('C5:D'), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
      }else{
          break; 
      }
    }
}

【问题讨论】:

  • 这个特定代码有什么问题?更具体地说明什么不起作用以及您收到的错误消息是什么。如果您正在寻找代码改进/建议,请在此处发布此问题:codereview.stackexchange.com
  • 您的 copyTo 的源和目标范围相同

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


【解决方案1】:

比较每个日期的开始日期值

function lockDaysValues (){
  var ss=SpreadsheetApp.getActive();
  var dt=new Date(); 
  var dtv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
  var sheets=ss.getSheets(); 
  for(var x=0;x<sheets.length;x++){
    sdt=new Date(sheets[x].getRange('C2').getValue());//presume this is date  
    sdtv=new Date(sdt.getFullYear(),sdt.getMonth(),sdt.getDate()).valueOf();
    if(sdtv==dtv){
      //source and destination seem to be the same so not sure what you want here
      sheets[x].getRange(5,3,sheets[x].getLastRow()-4,2).copyTo(sheets[x].getRange(5,3,sheets[x].getLastRow()-4,2), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    }else{
      break; 
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多