【问题标题】:How to apply a macro to all sheets in one spreadsheet in Google Sheets如何将宏应用于 Google 表格中一个电子表格中的所有表格
【发布时间】:2019-03-17 21:39:31
【问题描述】:

我在 google 表格中为我的电子表格中的一张表格记录了宏。我想编辑脚本,使其适用于电子表格中的所有工作表(这样我就不必分别在每个工作表中运行记录的宏)。我看过有关如何在 Excel 中执行此操作的文章,但在 Google 表格中是否可行?

【问题讨论】:

  • 分享你的宏
  • 感谢您回答我的问题,虽然我不太明白您的回答。我认为共享只适用于其他用户。您如何与您自己的工作簿中的其他选项卡/电子表格共享内容(在 Google 表格中)。

标签: google-apps-script google-sheets google-sheets-api google-sheets-macros


【解决方案1】:

您确定它不适用于其他工作表吗?录制的宏通常使用活动工作表而不是命名工作表。

【讨论】:

  • 它的工作原理是我每次都必须运行它。这个特定的工作簿中有很多电子表格。我希望将宏设置为自动在工作簿(谷歌工作表)中的每个工作表中运行,而无需“手动”运行。
  • 函数 Subjectsthirdterm() { var spreadsheet = SpreadsheetApp.getActive();电子表格.getRange('A54').activate();电子表格.getCurrentCell().setValue('数学 3');电子表格.getRange('A55:A58').activate();电子表格.getRange('A54').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false); };
【解决方案2】:

虽然我迟到了 9 个月才回答这个问题,但这里有一个对您的问题的有效答案,其中有一个示例,可以使用另一个名为 ClearActiveSheet 的函数清除所有可见工作表。如果有人仍在寻找如何做到这一点,这应该给出一个想法。

function ClearActiveSheet() {

  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();

  sheet.getRange('A1').activate();  
  sheet.clear();  
};

function ClearAllVisibleSheets() {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(function(sheet) {    
    //You would need to make this the current active sheet, so that you can call the ClearActiveSheet function that works on active sheet
    sheet.activate()

    // Now since you have this sheet activated, you can call the function to clear the active sheet
    ClearActiveSheet();   
  });
};

从 Tools-> ScriptsEditor -> SelectFunction (ClearAllVisibleSheets) 中选择 GoogleSpredsheets 中的 ClearAllVisibleSheets 函数并运行/或调试,您将看到它完成了这项工作。

P.S:如果这满足您的需求,请记得点赞并接受它作为答案:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多