1. 程式人生 > 實用技巧 >谷歌表格_如何使用巨集自動執行Google表格

谷歌表格_如何使用巨集自動執行Google表格

谷歌表格

谷歌表格

Google Sheets lets you automate repetitive tasks with macros, and you can then bind them to keyboard shortcuts to execute them quickly. They work using Google Apps Script to capture your actions for later use.

Google表格可以讓您使用巨集自動執行重複性任務,然後可以將其繫結到鍵盤快捷鍵以快速執行它們。 他們使用Google Apps指令碼來捕獲您的操作以供以後使用。

什麼是巨集? (What are Macros?)

A macro—or macroinstruction—is a specific sequence of actions that let you automate a series of steps to increase productivity. They work by recording your actions and saving them into a file that’s bound to the spreadsheet in which they were recorded.

巨集(或巨集指令)是特定的操作序列,可讓您自動化一系列步驟以提高生產率。 它們的工作方式是記錄您的操作並將其儲存到與記錄了這些操作的電子表格繫結的檔案中。

When you record a macro in Google Sheets, it automatically creates an Apps Scriptwith all the code to replicate your actions for you. This means you can create complex macros without knowing how to write code. The next time you run it, Sheets will do everything you did when you recorded the macro. Essentially, you’re teaching Google Sheets how to manipulate a document to your liking with a single command.

當您在Google表格中記錄巨集時,它會自動建立一個包含所有程式碼的Apps指令碼,以為您複製操作。 這意味著您可以在不知道如何編寫程式碼的情況下建立複雜的巨集。 下次執行它時,Sheets將完成您在錄製巨集時所做的所有操作。 本質上,您是在教Google表格如何通過單個命令來按自己的喜好操縱文件。

Macros are a powerful feature that can do basically anything Sheets is capable of doing. Here are just a few examples of its functionality:

巨集是一項強大的功能,基本上可以完成Sheets能夠執行的任何操作。 以下是其功能的一些示例:

  • Apply formatting and styles.

    應用格式和樣式。
  • Create completely new spreadsheets.

    建立全新的電子表格。
  • Use any Google Sheets function, toolbar, menu, or feature.

    使用任何Google表格功能,工具欄,選單或功能。

The sky is the limit.

天空才是極限。

如何在Google表格中記錄巨集 (How to Record a Macro in Google Sheets)

Fire up a Google Sheet and click Tools > Macros > Record Macro.

啟動Google表格,然後單擊工具>巨集>記錄巨集。

Click Tools > Macros > Record Macro

This opens the recording menu at the bottom of the window, with two options for capturing your actions:

這將開啟視窗底部的“錄製”選單,其中有兩個用於捕獲動作的選項:

  • Absolute References:The macro will do tasks only on the exact cells you record. If you italicize cell B1, the macro will only ever italicize B1 regardless of what cell you clicked on.

    絕對引用:巨集將僅在您記錄的確切單元格上執行任務。 如果您將單元格B1變為斜體,則無論您單擊哪個單元格,巨集都只會將其斜體為B1。

  • Relative References:The macro will do tasks on the selected cells, regardless of where they are in the sheet. If you italicize B1 and C1, you can re-use the same macro to italicize cells D1 and E1 later.

    相關參考:巨集將在選定的單元格上執行任務,無論它們在表中的位置如何。 如果將B1和C1斜體化,則可以在以後重新使用同一巨集來斜體化單元格D1和E1。

Select whether you want an absolute or relative reference, then you can start to click, format, and teach Sheets what order you want these actions to replicate.

選擇您要的是絕對參考還是相對參考,然後可以單擊,格式化和教導Sheets您希望這些操作複製的順序。

Choose either Absolute References or Relative References

After you’ve captured all the actions for this macro, click “Save.”

在捕獲了該巨集的所有動作之後,單擊“儲存”。

Click Save when you're done recording

Enter a name for your macro.Google also lets you create shortcuts for up totenmacros. Ifyou want to bind a macro to a keyboard shortcut, enter a number from 0-9 in the space provided. When you finish, click “Save.”

輸入巨集的名稱。 Google還允許您建立最多十個巨集如果要將巨集繫結到鍵盤快捷鍵,請在提供的空格中輸入0-9之間的數字。 完成後,單擊“儲存”。

Enter a name for your macro, then click Save

If you need to tweak your macro’s name or shortcut, you can edit a macro by clicking Tools > Macros > Manage Macros.

如果需要調整巨集的名稱或快捷方式,可以通過單擊工具>巨集>管理巨集來編輯巨集。

To create a keyboard shortcut, click Tools > Macros > Manage Macros

From the window that opens, tweak as desired and then click “Update.”

在開啟的視窗中,根據需要進行調整,然後單擊“更新”。

Beside each macro, enter a number from 0-9 to bind to a shortcut

The next time you press the shortcut associated with the macro, it will run without having to open the macro menu from the toolbar.

下次您按下與巨集關聯的快捷方式時,它將執行,而無需從工具欄開啟巨集選單。

如何在Google表格中執行巨集 (How to Run a Macro in Google Sheets)

If your macro is an absolute reference, you can run the macro by pressing the keyboard shortcut or go toTools > Macros > Your Macro and then click the appropriate option.

如果您的巨集是絕對引用,則可以通過按鍵盤快捷鍵或轉到“工具”>“巨集”>“您的巨集”,然後單擊適當的選項來執行巨集。

Click Tools > Macros > YourMacroName to run your macro

Otherwise, if your macro is a relative reference, highlight the cells in your spreadsheet on which you want the macro to run and then press the corresponding shortcut, or click on it from Tools > Macros > Your Macro.

否則,如果您的巨集是相對引用,請突出顯示電子表格中您要在其上執行巨集的單元格,然後按相應的快捷方式,或者從“工具”>“巨集”>“您的巨集”中單擊它。

If you made a relative referenced macro, highlight the cells, then click Tools > Macros > YourMacroName, to run the macro

如何匯入巨集 (How to Import Macros)

As mentioned earlier, when you record a macro, it gets bound to the spreadsheet on which you recorded it. But what if you want to import a macro from another spreadsheet?While it’s not a straightforward and simple task, you can do it using this little workaround.

如前所述,當您記錄巨集時,它會繫結到記錄巨集的電子表格。 但是,如果要從另一個電子表格匯入巨集該怎麼辦? 儘管這不是一項簡單明瞭的任務,但是您可以使用此小解決方法來完成。

Because recorded macros are stored as functions in Google Apps Script, to import a macro, you need to copy the function and then paste it in the new sheet’s macro file.

由於錄製的巨集是作為函式儲存在Google Apps指令碼中的,因此要匯入巨集,您需要複製該函式,然後將其貼上到新工作表的巨集檔案中。

Open the Google Sheet with the macro you want to copy and then click on Tools > Macros > Manage Macros.

使用您要複製的巨集開啟Goog​​le表格,然後單擊工具>巨集>管理巨集。

Click on Tools > Macros > Manage Macros

Next, click the “More” icon next to the macro you’d like to copy and then click “Edit Script.”

接下來,點選您要複製的巨集旁邊的“更多”圖示,然後點選“編輯指令碼”。

Click the more icon, then click Edit Script

All macros save to the same file, so if you have a couple of macros saved, you may have to sift through them.The function’s name is the same one you gave it when you created it.

所有巨集都儲存到同一個檔案中,因此,如果您儲存了幾個巨集,則可能必須在它們之間進行篩選。 該函式的名稱與您建立函式時使用的名稱相同。

Highlight the macro(s) you want to copy, then press Ctrl + C.Be sure to copy everything up to and including the closing semi-colon.

突出顯示要複製的巨集,然後按Ctrl +C。確保將所有內容複製到包括分號在內的所有內容。

Highlight and copy the macro's function with Ctrl + C

Now, open the other spreadsheet you’ll be importing the macro to and click Tools > Macros > Record Macro.

現在,開啟要將巨集匯入到的其他電子表格,然後單擊工具>巨集>記錄巨集。

Create a new macro in your other spreadsheet. Click Tools > Macros > Record Macro

Immediately click “Save” without recording any actions to create a placeholder function in the sheet’s macro file for us. You’ll be deleting this a little later.

立即單擊“儲存”,而無需進行任何操作即可在工作表的巨集檔案中為我們建立佔位符功能。 您稍後會刪除它。

Click Save

Click “Save” again.

再次單擊“儲存”。

You don't need to worry about naming it, click Save

Open Google Apps Script by clicking Tools > Script Editor, and then open the macros.gs file from the left pane. Delete the existing function and then press Ctrl + V to paste in the macro from your other Sheet.

通過單擊工具>指令碼編輯器,開啟Goog​​le Apps指令碼,然後從左窗格中開啟macros.gs檔案。 刪除現有功能,然後按Ctrl + V從其他工作表貼上到巨集中。

In the macros.gs file, paste the macro's function from the first spreadsheet

Press Ctrl + S to save the script, close the tab, and return to your spreadsheet.

按Ctrl + S儲存指令碼,關閉選項卡,然後返回到電子表格。

Your spreadsheet reads the macros.gs file and looks for changes made to it. If a new function is detected, you can use the Import feature to add a macro from another sheet.

您的電子表格將讀取macros.gs檔案並查詢對其所做的更改。 如果檢測到新功能,則可以使用“匯入”功能從另一個工作表新增巨集。

Next, click Tools > Macros > Import.

接下來,單擊工具>巨集>匯入。

Click Tools > Macros > Import to import the macros to this document

Finally, click “Add Function” under the macro you want to add.

最後,在要新增的巨集下單擊“新增功能”。

Finally, click Add Function next to the macro you want to add

Unfortunately, you will have to bind the macro manually to a keyboard shortcut again. Just follow the instruction previously mentioned, and you’ll be all set to use this macro across multiple sheets.

不幸的是,您將不得不再次將巨集手動繫結到鍵盤快捷鍵。 只要按照前面提到的說明進行操作,就可以在多個工作表上使用此巨集。



That’s all there is to creating and using macros in Google Sheets. There’s no limit to the routines you can create for your spreadsheets to limit the amount of time you spend doing these repetitive tasks.

這就是在Google表格中建立和使用巨集的全部內容。 您可以為電子表格建立的例程沒有限制,以限制您花費在執行這些重複任務上的時間。

翻譯自: https://www.howtogeek.com/413334/how-to-automate-google-sheets-with-macros/

谷歌表格