Google apps script 如何使用谷歌应用程序脚本复制谷歌电子表格、重命名并将内容粘贴为绝对值

Google apps script 如何使用谷歌应用程序脚本复制谷歌电子表格、重命名并将内容粘贴为绝对值,google-apps-script,google-sheets,automation,Google Apps Script,Google Sheets,Automation,各位好,飞越者 我想在每个月的1.st复制一个名为“Dashboard”的工作表,工作表名称为上一个月和上一年(MMMM YYYY),内容为静态值 为了做到这一点,我试着调整以下两个脚本,但它没有做我想要的 问题是: 这些值不会粘贴为绝对值 工作表名称是当前月份的名称,而不是当前月份的名称 上个月 我相信对于问题编号1,下面的语句在语法上有错误,但我找不到: asn.copyTo(getRange("MMMMM yyyy"),{contentsOnly: true}); 对于第二个问题,我真的

各位好,飞越者

我想在每个月的1.st复制一个名为“Dashboard”的工作表,工作表名称为上一个月和上一年(MMMM YYYY),内容为静态值

为了做到这一点,我试着调整以下两个脚本,但它没有做我想要的

问题是:

  • 这些值不会粘贴为绝对值

  • 工作表名称是当前月份的名称,而不是当前月份的名称 上个月

  • 我相信对于问题编号1,下面的语句在语法上有错误,但我找不到:

    asn.copyTo(getRange("MMMMM yyyy"),{contentsOnly: true});
    
    对于第二个问题,我真的不知道如何开始

    =>一些想法?任何帮助都将不胜感激

    function duplicatesheet() {
      var as = SpreadsheetApp.getActiveSpreadsheet(); // active spreadsheet
    
        var s = as.getSheetByName('Dashboard'); // first sheet object
    
     // var s = as.getActiveSheet(); // first sheet object
      var dateCell = "H5"; // cell containing first date
      var N = 1; // number of copies to make
    
      var startDate = new Date(s.getRange(dateCell).getValue()); // get the date stored in dateCell
      var day = startDate.getDate(); // extract the day
      var month = startDate.getMonth(); // extract the month
      var year = startDate.getFullYear(); // extract the year
    
      // loop over N times
      for (var i = 0; i < N; i++) {
    
       var asn = s.copyTo(as); // make a duplicate of the first sheet
    
    
    
        var thisSheetDate = new Date(year, month, day+(i+1)); // store the new date as a variable temporarily
    
        asn.getRange(dateCell).setValue(thisSheetDate); // writes the date in cell "B3"
        asn.setName(Utilities.formatDate(thisSheetDate, undefined, "MMMMM yyyy")); // sets the name of the new sheet
        asn.copyTo(getRange("MMMMM yyyy"),{contentsOnly: true});
      }
    }
    

    每月第一天创建仪表板副本

    function createNewSheetFromTemplate(templatename="Dashboard") {
      const dt=new Date();
      //This runs the insert only on the first day of the month
      if(dt.getDate()==1) {
        const ss=SpreadsheetApp.getActive();
        const template=ss.getSheetByName(templatename);//default parameter
        const name=Utilities.formatDate(new Date(dt.getFullYear(),dt.getMonth(),0), Session.getScriptTimeZone(), "MMMM/yyyy");
        ss.insertSheet(name, {template:template});
      }
    }
    
    function createTimeBasedTrigger() {
      if(!isATrigger('createNewSheetFromTemplate')) {
        ScriptApp.newTrigger('createNewSheetFromTemplate').timeBased().everyDays(n).atHour(6).create();//The trigger happens daily at 6 am
      }
    }
    
    //You just need to run this once.   It's setup to  only allow one trigger to be created even if you run it again.
    function isATrigger(funcName){
      var r=false;
      if(funcName){
        var allTriggers=ScriptApp.getProjectTriggers();
        for(let i=0;i<allTriggers.length;i++){
          if(funcName==allTriggers[i].getHandlerFunction()){
            r=true;
            break;
          }
        }
      }
      return r;
    }
    
    函数createNewSheetFromTemplate(templatename=“Dashboard”){ 常数dt=新日期(); //这仅在每月的第一天运行插入 如果(dt.getDate()==1){ const ss=SpreadsheetApp.getActive(); const template=ss.getSheetByName(templatename);//默认参数 const name=Utilities.formatDate(新日期(dt.getFullYear(),dt.getMonth(),0),Session.getScriptTimeZone(),“MMMM/yyyy”); 插入页(名称,{template:template}); } } 函数createTimeBasedTrigger(){ 如果(!isATrigger('createNewSheetFromTemplate')){ ScriptApp.newTrigger('createNewSheetFromTemplate').timeBased().everyDays(n).atHour(6.create();//触发器每天早上6点触发 } } //你只需要运行一次。它的设置只允许创建一个触发器,即使您再次运行它。 函数isATrigger(funcName){ var r=假; 如果(名称){ var allTriggers=ScriptApp.getProjectTriggers();
    对于(让i=0;i在每个月的第一天创建仪表板副本

    function createNewSheetFromTemplate(templatename="Dashboard") {
      const dt=new Date();
      //This runs the insert only on the first day of the month
      if(dt.getDate()==1) {
        const ss=SpreadsheetApp.getActive();
        const template=ss.getSheetByName(templatename);//default parameter
        const name=Utilities.formatDate(new Date(dt.getFullYear(),dt.getMonth(),0), Session.getScriptTimeZone(), "MMMM/yyyy");
        ss.insertSheet(name, {template:template});
      }
    }
    
    function createTimeBasedTrigger() {
      if(!isATrigger('createNewSheetFromTemplate')) {
        ScriptApp.newTrigger('createNewSheetFromTemplate').timeBased().everyDays(n).atHour(6).create();//The trigger happens daily at 6 am
      }
    }
    
    //You just need to run this once.   It's setup to  only allow one trigger to be created even if you run it again.
    function isATrigger(funcName){
      var r=false;
      if(funcName){
        var allTriggers=ScriptApp.getProjectTriggers();
        for(let i=0;i<allTriggers.length;i++){
          if(funcName==allTriggers[i].getHandlerFunction()){
            r=true;
            break;
          }
        }
      }
      return r;
    }
    
    函数createNewSheetFromTemplate(templatename=“Dashboard”){ 常数dt=新日期(); //这仅在每月的第一天运行插入 如果(dt.getDate()==1){ const ss=SpreadsheetApp.getActive(); const template=ss.getSheetByName(templatename);//默认参数 const name=Utilities.formatDate(新日期(dt.getFullYear(),dt.getMonth(),0),Session.getScriptTimeZone(),“MMMM/yyyy”); 插入页(名称,{template:template}); } } 函数createTimeBasedTrigger(){ 如果(!isATrigger('createNewSheetFromTemplate')){ ScriptApp.newTrigger('createNewSheetFromTemplate').timeBased().everyDays(n).atHour(6.create();//触发器每天早上6点触发 } } //你只需要运行一次。它的设置只允许创建一个触发器,即使你再次运行它。 函数isATrigger(funcName){ var r=假; 如果(名称){ var allTriggers=ScriptApp.getProjectTriggers(); 对于(i=0;i)您的目标:
  • 将上月的值保存在具有上月名称的工作表中
  • 这个月的第一天做这个
  • 继续使用当前月份的现有仪表板
  • 下个月再做一次

  • 实现这些目标的一个例子: 函数onMonthStart(){ const monthNames=[“一月”、“二月”、“三月”、“四月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月]; //上个月叫什么名字? var thisMonth=Utilities.formatDate(新日期(),“GMT”,“MMM”); var pastMonth=monthNames[monthNames.indexOf(thisMonth)-1]; 如果(本月的月平均指数)=0){ 过去月份='十二月'; } //为上个月的值创建新的目标工作表 var ss=SpreadsheetApp.getActiveSpreadsheet(); var pastMonthSheet=ss.insertSheet(pastMonth); //从仪表板复制数据 var pastMonthDataRange=ss.getSheetByName('Dashboard').getDataRange(); //粘贴上个月的工作表 pastMonthSheet.activate().getRange(pastMonthDataRange.getA1Notation()).setValues(pastMonthDataRange.getValues()) } 如何在每月的第一天触发onMonthStart
    
    在每月的第一天安装。
    


    注意事项:

  • Using方法将对象作为参数。您正在传递带有选项的范围。不一样
  • 尽量保持变量命名风格的一致性,并使用具有可读性含义的名称
  • 你的目标:
  • 将上月的值保存在具有上月名称的工作表中
  • 这个月的第一天做这个
  • 继续使用当前月份的现有仪表板
  • 下个月再做一次

  • 实现这些目标的一个例子: 函数onMonthStart(){ const monthNames=[“一月”、“二月”、“三月”、“四月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月]; //上个月叫什么名字? var thisMonth=Utilities.formatDate(新日期(),“GMT”,“MMM”); var pastMonth=monthNames[monthNames.indexOf(thisMonth)-1]; 如果(本月的月平均指数)=0){ 过去月份='十二月'; } //为上个月的值创建新的目标工作表 var ss=SpreadsheetApp.getActiveSpreadsheet(); var pastMonthSheet=ss.insertSheet(pastMonth); //从仪表板复制数据 var pastMonthDataRange=ss.getSheetByName('Dashboard').getDataRange(); //粘贴上个月的工作表 pastMonthSheet.activate().getRange(pastMonthDataRange.getA1Notation()).setValues(pastMonthDataRange.getValues()) }
    如何在每月的第一天触发onMonthStart
    
    在每月的第一天安装。
    


    注意事项:

  • Using方法将对象作为参数。您正在传递带有选项的范围。不一样
  • 尽量保持变量命名风格的一致性,并使用具有可读性含义的名称

  • @库珀和@Aerials非常感谢您的贡献!这两款产品都是非常优雅的解决方案

    最终,我合并了这两个脚本,以便能够复制选项卡,并将格式和值保留为绝对值

    这就是它的样子:

    /// Duplicate Dashboard values in previous month tab
    
    function createNewSheetFromTemplate(templatename="Dashboard") {
      const dt=new Date();
      //This runs the insert only on the first day of the month
      if(dt.getDate()==1) {
        const ss=SpreadsheetApp.getActive();
        const template=ss.getSheetByName(templatename);//default parameter
        const name="Pacing " + Utilities.formatDate(new Date(dt.getFullYear(),dt.getMonth(),0), Session.getScriptTimeZone(), "MMMM");
        ss.insertSheet(name, {template:template});
    
     // Select the new created sheet already renamed    
         var pastMonthSheet = ss.getSheetByName(name);
     // Copy values from dashboard
      var pastMonthDataRange = ss.getSheetByName(name).getDataRange();
    
      // Paste in the values form the dashboard as absolute values in the past month sheet
      pastMonthSheet.activate().getRange(pastMonthDataRange.getA1Notation()).setValues(pastMonthDataRange.getValues())
    
      }
    }
    

    @库珀和@Aerials非常感谢你