SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

⭐ Part 3: Starting from Zero! Google Sheets × JavaScript Introduction: 'Move it with a Click! Your First Macro Button'

First, link the script you created so far to a button and run it

  • Experience the initial authorization

  • As an application, add a process to write values to cells


Practice 1: Assigning a script to a button

  1. Open 'Practice 1' from Google Drive

  2. Open the [Extensions] → [Apps Script] menu

  3. Rewrite the code inside as follows and save it (💾 mark or Ctrl + S)

function gogo() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange("A1").setValue("処理完了!");
}

4. Return to the spreadsheet screen (switch tabs or close the Apps Script tab)
5. Right-click the button (shape/image) you created, and a vertical three-dot (︙) menu will appear in the top right. Click it → select [Assign script]

6. Enter "gogo" in the input field and click OK
7. Click the button


Practice 2: The flow of initial authorization

1. Return to the spreadsheet and click the button
2. A pop-up saying "Authorization required" like the one below will be displayed
 ・Click OK

3. A Google account selection screen will appear, so select your account

4. If a warning saying "This app is not verified by Google" appears,
    [Advanced] click

Click "Go to (unsafe page)" that appears at the bottom

✴️ This is a confirmation to "allow spreadsheet operations" for the script you created yourself. Once you allow it, this authorization will not appear next time.

Click "Continue"

Once authorization is complete, click the button again → If a success log is displayed, you're good to go!

🔒 Authorization is only for the first time. From the next time onwards, just press the button!


Practice 3: Advanced (Writing to cells)

From here on, it's a bit more advanced. This time, let's try a process where text is written to a cell in the sheet when the button is pressed.

Paste the following code into the editor and save it.
Don't forget to save!

function gogo() {
  SpreadsheetApp.getUi().alert("ボタンの実行に成功しました!");
}
  1. Return to the spreadsheet and assign "gogo" to the button

  2. Click the button → If the message appears, you've succeeded 🎉


Common pitfalls (Part 3)

  • Typo in the function name → Correct it to gogo

  • Forgot to save after changing the script → 💾Save before assigning/running

  • Changed the script but it doesn't run when clicking the button → You may need to 're-assign' the script


Cleanup & Peace of Mind Tips

  • You can revoke granted permissions at any time from your Google Account under [Security] → [Third-party access management]

  • You can remove the button on the sheet at any time by right-clicking and selecting [Delete]


Application Tips (Just rewrite the code)

  • Delete blank rows

  • Copy values to another sheet

  • Automatically enter today's date

  • Change cell colors based on conditions

Once you have a mechanism where processing runs when clicked, you can apply it in various ways just by replacing the contents of onButtonClick().


#GoogleAppsScript #JavaScriptIntroduction
#CloudUtilization
#BeginnerProgramming
#SpreadsheetAutomation


いいなと思ったら応援しよう!

風369 🌿チップでの応援、ほんの少しでも嬉しいです。いただいたご支援は、新しい記事づくりの糧にします。