【问题标题】:Google script : Sheet duplicate + rename and move to the end谷歌脚本:工作表重复+重命名并移到最后
【发布时间】:2022-11-10 21:24:15
【问题描述】:

我有一个简单的发票系统,我唯一的问题是我不知道如何将多个脚本组合成一个。我想要的是,当我按下创建发票以制作打印表的副本并相应地重命名时(发票编号 + 客户名称,假设 B12 单元格包含客户名称和 G12 发票编号,因此表名称将是“詹姆斯邦德 007") 之后,该表将移动到表的末尾。 到目前为止,我使用这些脚本来实现复制并移动到最后

function Dupewithvaluesonly() {
  const range = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet().getDataRange();
 
  range.copyTo(range, {formatOnly: true});  // Added
  range.copyTo(range, {contentsOnly: true});
  
}

function MovetoEnd() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.moveActiveSheet(ss.getNumSheets());
}


【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    我相信你的目标如下。

    • 您想要复制活动工作表并使用活动工作表的单元格“B12”和“G12”中的值设置工作表名称。
    • 另外,想要将单元格值转换为静态值。
    • 您希望通过合并 2 个函数来实现此目的。

    在这种情况下,如何进行以下修改?

    修改后的脚本:

    function myFunction() {
      // Copy the active sheet.
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const sheet = ss.duplicateActiveSheet();
    
      // Retrieve the values from cells "B12" and "G12". And set the sheet name.
      const [b12, , , , , g12] = sheet.getRange("B12:G12").getValues()[0];
      sheet.setName(`${g12} ${b12}`);
    
      // Convert values to static values.
      const range = sheet.getDataRange();
      range.copyTo(range, { formatOnly: true }); // This is from your script.
      range.copyTo(range, { contentsOnly: true }); // This is from your script.
    
      // Move the copied sheet to the last tab.
      ss.moveActiveSheet(ss.getNumSheets()); // This is from your script.
    }
    

    笔记:

    • 如果您想使用特定工作表而不是活动工作表,请修改上述脚本如下。

      •   const sheet = ss.duplicateActiveSheet();
        
      •   const sheet = ss.getSheetByName("Sheet1").copyTo(ss).activate(); // Please set the sheet name to "Sheet1".
        

    【讨论】:

    • 它就像一个魅力。谢谢一百万,你让我的生活变得超级轻松。尊重
    • @etnevel 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多