Google Apps Script: Dev Tools, Color Picker, Side Bar, Custom Prompt, HtmlService, onOpen, Sidebar, Dialog Box
I wanted to update one of my free Google Add-on apps that works with colour. What I had is just the standard HTML color input element where the user selects from the palette and that hexadecimal colour code is returned to Google Apps Script to be used in the App. The problem is that it is really hard to get a good colour match between the palette and Google’s own colour range that is accessible from the fill or text colour buttons.
Take a look at the comparison between the HTML color input element and the Google Sheet background colour palette in the image below.
That’s not a user-friendly tool to match colours with the standard Google palette.
So in the back of my mind, I had always wanted to create a tool for a sidebar or dialogue box that would allow the user to easily access the standard colours or use the custom palette provided by the HTML color input.
After finding a bit of time in my recent summer break I came up with this.
And this is how it looks in action:
(Note: add-ons has been updated to Extensions)
In the above gif, I have run a basic demonstration on how to use the colour picker in a custom sidebar and custom dialogue box in Google Sheets.
You can incorporate this colour picker into any sidebar or custom dialogue project along with other form data and then extract the values for your Google Apps Script project.
Features
This colour picker contains the following features:
- Dropdown menu for colour selection so as not to clutter the sidebar or dialogue.
- Colour radio buttons to match Google’s standard colour palette for Sheets, Docs and Slides.
- Custom colour button for the user to select their own colour using the HTML color input.
- The whole colour picker can be put in a single separate HTML file.
- Two file types:
- One colour selection file that contains CSS suited for a plain dialogue box styling.
- One colour selection file that takes into account Google’s CSS package for Add-ons.
- Hover features to easily distinguish which button is being focussed on.
- Standard plain Javascript. No other libraries added.
Contents
Access
I think there are 3 important pieces of data worth retrieving from the colour picker:
Hexadecimal Code
You can use this code to set background, text and border colours in your Google Sheet, Doc or Slide. To access this value in your HTML file:
document.getElementById("color-result-hex").innerText;
Google Color Name
This might be useful if you want to display the Google-assigned name of the colour:
document.getElementById("color-result-name").innerText;
Text Color
I use white text for dark hex colours and black text for light hex colours. This might be useful for setting a complementary text colour to a background.
document.getElementById("color-result").style.color;
Code
The HTML file I’ve named picker.html. The file contains all the HTML, CSS and Javascript used to create the colour picker widget.
As mentioned earlier, I have two different HTML files depending on what styling you want to use it with.
Colour Picker – Standard
This file should be used if you are not also incorporating Google’s CSS package for Add-ons.
picker.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
<style> .google-color-palette { -webkit-appearance: none; -moz-appearance: none; appearance: none; display: inline-block; position: relative; top: 10px; height: 20px; width: 20px; border: 0; border-radius: 50px; cursor: pointer; margin-right: -2px; margin-bottom: -1px; margin-top: -1px; outline: none; } .google-color-palette:checked::before { position: absolute; font: 13px/1 "Open Sans", sans-serif; left: 6.5px; top: 2px; content: "\02143"; transform: rotate(40deg); } .google-color-palette:hover { box-shadow: 2px 2px 2px grey; } .hidden-col-picker { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; } .color-pick { margin-top: 10px; } .col-pick-button { border: 0; background-color: transparent; } .col-pick-button:hover { color: #6d9eeb; } .color-result { max-width: 220px; font: 13px/1 "Open Sans", sans-serif; color: #000000; background-color: #ffffff; border: 0; border-radius: 4px; padding: 5px; box-shadow: 2px 2px 2px grey; text-align: center; } .color-result:hover { box-shadow: 2px 2px 2px #434343; cursor: pointer; } .color-expander { display: inline-block; transform: scale(1.5, 1); float: right; padding: 1px 1px; } .color-picker { display: none; } </style> <div class="color-result" id="color-result" onclick="expander()"> <span id="color-result-hex">Select a </span> <span id="color-result-name">color</span> <span class="color-expander" id="color-result-expand" ><strong>ᐯ</strong></span > </div> <div class="color-picker" id="color-picker"> <input type="radio" name="GoogleColorPalette" value="#000000" id="black" class="google-color-palette" style="background-color:#000000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#434343" id="dark-grey-4" class="google-color-palette" style="background-color:#434343;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#666666" id="dark-grey-3" class="google-color-palette" style="background-color:#666666;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#999999" id="dark-grey-2" class="google-color-palette" style="background-color:#999999;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#b7b7b7" id="dark-grey-1" class="google-color-palette" style="background-color:#b7b7b7;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#cccccc" id="grey" class="google-color-palette" style="background-color:#cccccc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d9d9d9" id="light-grey-1" class="google-color-palette" style="background-color:#d9d9d9;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#efefef" id="light-grey-2" class="google-color-palette" style="background-color:#efefef;color:#000000;border:#d9d9d9 solid 1px" /><input type="radio" name="GoogleColorPalette" value="#f3f3f3" id="light-grey-3" class="google-color-palette" style="background-color:#f3f3f3;color:#000000;border:#d9d9d9 solid 1px" /><input type="radio" name="GoogleColorPalette" value="#ffffff" id="white" class="google-color-palette" style="background-color:#ffffff;color:#000000;border:#d9d9d9 solid 1px" /><br /><input type="radio" name="GoogleColorPalette" value="#980000" id="red-berry" class="google-color-palette" style="background-color:#980000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#ff0000" id="red" class="google-color-palette" style="background-color:#ff0000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#ff9900" id="orange" class="google-color-palette" style="background-color:#ff9900;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ffff00" id="yellow" class="google-color-palette" style="background-color:#ffff00;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#00ff00" id="green" class="google-color-palette" style="background-color:#00ff00;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#00ffff" id="cyan" class="google-color-palette" style="background-color:#00ffff;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#4a86e8" id="cornflower-blue" class="google-color-palette" style="background-color:#4a86e8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#0000ff" id="blue" class="google-color-palette" style="background-color:#0000ff;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#9900ff" id="purple" class="google-color-palette" style="background-color:#9900ff;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#ff00ff" id="magenta" class="google-color-palette" style="background-color:#ff00ff;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#e6b8af" id="light-red-berry-3" class="google-color-palette" style="background-color:#e6b8af;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#f4cccc" id="light-red-3" class="google-color-palette" style="background-color:#f4cccc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#fce5cd" id="light-orange-3" class="google-color-palette" style="background-color:#fce5cd;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#fff2cc" id="light-yellow-3" class="google-color-palette" style="background-color:#fff2cc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d9ead3" id="light-green-3" class="google-color-palette" style="background-color:#d9ead3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d0e0e3" id="light-cyan-3" class="google-color-palette" style="background-color:#d0e0e3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#c9daf8" id="light-cornflower-blue-3" class="google-color-palette" style="background-color:#c9daf8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#cfe2f3" id="light-blue-3" class="google-color-palette" style="background-color:#cfe2f3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d9d2e9" id="light-purple-3" class="google-color-palette" style="background-color:#d9d2e9;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ead1dc" id="light-magenta-3" class="google-color-palette" style="background-color:#ead1dc;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#dd7e6b" id="light-red-berry-2" class="google-color-palette" style="background-color:#dd7e6b;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ea9999" id="light-red-2" class="google-color-palette" style="background-color:#ea9999;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#f9cb9c" id="light-orange-2" class="google-color-palette" style="background-color:#f9cb9c;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ffe599" id="light-yellow-2" class="google-color-palette" style="background-color:#ffe599;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#b6d7a8" id="light-green-2" class="google-color-palette" style="background-color:#b6d7a8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#a2c4c9" id="light-cyan-2" class="google-color-palette" style="background-color:#a2c4c9;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#a4c2f4" id="light-cornflower-blue-2" class="google-color-palette" style="background-color:#a4c2f4;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#9fc5e8" id="light-blue-2" class="google-color-palette" style="background-color:#9fc5e8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#b4a7d6" id="light-purple-2" class="google-color-palette" style="background-color:#b4a7d6;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d5a6bd" id="light-magenta-2" class="google-color-palette" style="background-color:#d5a6bd;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#cc4125" id="light-red-berry-1" class="google-color-palette" style="background-color:#cc4125;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#e06666" id="light-red-1" class="google-color-palette" style="background-color:#e06666;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#f6b26b" id="light-orange-1" class="google-color-palette" style="background-color:#f6b26b;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ffd966" id="light-yellow-1" class="google-color-palette" style="background-color:#ffd966;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#93c47d" id="light-green-1" class="google-color-palette" style="background-color:#93c47d;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#76a5af" id="light-cyan-1" class="google-color-palette" style="background-color:#76a5af;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#6d9eeb" id="light-cornflower-blue-1" class="google-color-palette" style="background-color:#6d9eeb;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#6fa8dc" id="light-blue-1" class="google-color-palette" style="background-color:#6fa8dc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#8e7cc3" id="light-purple-1" class="google-color-palette" style="background-color:#8e7cc3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#c27ba0" id="light-magenta-1" class="google-color-palette" style="background-color:#c27ba0;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#a61c00" id="dark-red-berry-1" class="google-color-palette" style="background-color:#a61c00;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#cc0000" id="dark-red-1" class="google-color-palette" style="background-color:#cc0000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#e69138" id="dark-orange-1" class="google-color-palette" style="background-color:#e69138;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#f1c232" id="dark-yellow-1" class="google-color-palette" style="background-color:#f1c232;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#6aa84f" id="dark-green-1" class="google-color-palette" style="background-color:#6aa84f;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#45818e" id="dark-cyan-1" class="google-color-palette" style="background-color:#45818e;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#3c78d8" id="dark-cornflower-blue-1" class="google-color-palette" style="background-color:#3c78d8;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#3d85c6" id="dark-blue-1" class="google-color-palette" style="background-color:#3d85c6;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#674ea7" id="dark-purple-1" class="google-color-palette" style="background-color:#674ea7;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#a64d79" id="dark-magenta-1" class="google-color-palette" style="background-color:#a64d79;color:#ffffff" /><br /><input type="radio" name="GoogleColorPalette" value="#85200c" id="dark-red-berry-2" class="google-color-palette" style="background-color:#85200c;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#990000" id="dark-red-2" class="google-color-palette" style="background-color:#990000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#b45f06" id="dark-orange-2" class="google-color-palette" style="background-color:#b45f06;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#bf9000" id="dark-yellow-2" class="google-color-palette" style="background-color:#bf9000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#38761d" id="dark-green-2" class="google-color-palette" style="background-color:#38761d;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#134f5c" id="dark-cyan-2" class="google-color-palette" style="background-color:#134f5c;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#1155cc" id="dark-cornflower-blue-2" class="google-color-palette" style="background-color:#1155cc;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#0b5394" id="dark-blue-2" class="google-color-palette" style="background-color:#0b5394;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#351c75" id="dark-purple-2" class="google-color-palette" style="background-color:#351c75;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#741b47" id="dark-magenta-2" class="google-color-palette" style="background-color:#741b47;color:#ffffff" /><br /><input type="radio" name="GoogleColorPalette" value="#5b0f00" id="dark-red-berry-3" class="google-color-palette" style="background-color:#5b0f00;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#660000" id="dark-red-3" class="google-color-palette" style="background-color:#660000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#783f04" id="dark-orange-3" class="google-color-palette" style="background-color:#783f04;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#7f6000" id="dark-yellow-3" class="google-color-palette" style="background-color:#7f6000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#274e13" id="dark-green-3" class="google-color-palette" style="background-color:#274e13;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#0c343d" id="dark-cyan-3" class="google-color-palette" style="background-color:#0c343d;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#1c4587" id="dark-cornflower-blue-3" class="google-color-palette" style="background-color:#1c4587;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#073763" id="dark-blue-3" class="google-color-palette" style="background-color:#073763;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#20124d" id="dark-purple-3" class="google-color-palette" style="background-color:#20124d;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#4c1130" id="dark-magenta-3" class="google-color-palette" style="background-color:#4c1130;color:#ffffff" /><br /> <div class="color-pick"> <input type="color" id="col-picker" tabindex="-1" class="hidden-col-picker" /> <input type="button" id="col-pick-button" class="col-pick-button" value="Custom" /> </div> </div> <script> //#### Color selection expander #### function expander() { var arrow = document.getElementById("color-result-expand").innerText; if (arrow === "ᐯ") { document.getElementById("color-picker").style.display = "block"; document.getElementById("color-result-expand").innerHTML = "<strong>ᐱ</strong>"; } else { document.getElementById("color-picker").style.display = "none"; document.getElementById("color-result-expand").innerHTML = "<strong>ᐯ</strong>"; } } //#### Color Picker #### //When "Custom" is clicked is goes to the color menu. document .getElementById("col-pick-button") .addEventListener("click", function() { document.getElementById("col-picker").focus(); document.getElementById("col-picker").value = "#ffffff"; document.getElementById("col-picker").click(); }); //Check when color picker is used. document .getElementById("col-picker") .addEventListener("change", watchColorPicker); function watchColorPicker(e) { //Uncheck radio button. let checked = document.querySelector('input[name="GoogleColorPalette"]:checked') !== null; if (checked) { document.querySelector( 'input[name="GoogleColorPalette"]:checked' ).checked = false; } //Get the selected hex color let col_value = event.target.value; //Determin if dark or light color // https://codepen.io/andreaswik/pen/YjJqpK function blackOrWhite(hexcolor) { hexcolor = hexcolor.replace("#", ""); var r = parseInt(hexcolor.substr(0, 2), 16); var g = parseInt(hexcolor.substr(2, 2), 16); var b = parseInt(hexcolor.substr(4, 2), 16); var yiq = (r * 299 + g * 587 + b * 114) / 1000; return yiq >= 128 ? "black" : "white"; } let text_col = blackOrWhite(col_value); //Change color colorSelected(col_value, "Custom", text_col); } //#### Color Radio Buttons #### //Get the value of the checked button. function radioClick() { let value = this.value; let id = this.id; let text_col = this.style.color; colorSelected(value, id, text_col); } document .querySelectorAll("input.google-color-palette") .forEach(button => { button.addEventListener("click", radioClick); }); //#### Dislplay Color #### function colorSelected(value, id, text_col) { //get the id document.getElementById("color-result-hex").textContent = value; document.getElementById("color-result-name").textContent = ", " + id; const result = document.getElementById("color-result"); result.style.color = text_col; result.style.backgroundColor = value; //add color val } </script> |
Colour Picker – Supports Google’s CSS package for Add-ons
This one is the one you will probably use if you are building an Add-on for your organization or Google’s Marketplace.
This is also the one I will be using in the example below and is also available in the example Google Sheet and attached script file.
picker.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
<!-- Add a standard color picker access hex color: document.getElementById("color-result-hex").innerText access Google palette color name: document.getElementById("color-result-name").innerText access text color: document.getElementById("color-result").style.color; --> <style> .google-color-palette { -webkit-appearance: none !important; -moz-appearance: none !important; appearance: none !important; display: inline-block !important; position: relative !important; top: 10px !important; height: 20px !important; width: 20px !important; border: 0 !important; border-radius: 50px !important; cursor: pointer !important; margin-right: 3.5px !important; margin-bottom: -1px !important; margin-top: -1px !important; outline: none !important; } .google-color-palette:checked::before { position: absolute !important; font: 13px/1 "Open Sans", sans-serif !important; left: 6.5px !important; top: 2px !important; content: "\02143" !important; transform: rotate(40deg) !important; } .google-color-palette:checked::after { background:transparent !important; } .google-color-palette:hover { box-shadow: 2px 2px 2px grey !important; } .hidden-col-picker { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; } .color-pick { margin-top: 10px; } .col-pick-button { padding: 0px 0px 0px 0px !important; cursor: pointer !important; border: 0 !important; background-color: transparent !important; background: transparent !important; text-align:left !important; } .col-pick-button:hover { color: #6d9eeb !important; } .color-result { max-width: 220px; font: 13px/1 "Open Sans", sans-serif; color: #000000; background-color: #ffffff; border: 0; border-radius: 4px; padding: 5px; box-shadow: 2px 2px 2px grey; text-align: center; } .color-result:hover { box-shadow: 2px 2px 2px #434343; cursor: pointer; } .color-expander { display: inline-block; transform: scale(1.5, 1); float: right; padding: 1px 1px; } .color-picker { display: none; } </style> <div class="color-result" id="color-result" onclick="expander()"> <span id="color-result-hex">Select a </span>: <span id="color-result-name">color</span> <span class="color-expander" id="color-result-expand" ><strong>ᐯ</strong></span > </div> <div class="color-picker" id="color-picker"> <input type="radio" name="GoogleColorPalette" value="#000000" id="black" class="google-color-palette" style="background-color:#000000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#434343" id="dark-grey-4" class="google-color-palette" style="background-color:#434343;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#666666" id="dark-grey-3" class="google-color-palette" style="background-color:#666666;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#999999" id="dark-grey-2" class="google-color-palette" style="background-color:#999999;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#b7b7b7" id="dark-grey-1" class="google-color-palette" style="background-color:#b7b7b7;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#cccccc" id="grey" class="google-color-palette" style="background-color:#cccccc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d9d9d9" id="light-grey-1" class="google-color-palette" style="background-color:#d9d9d9;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#efefef" id="light-grey-2" class="google-color-palette" style="background-color:#efefef;color:#000000;border:#d9d9d9 solid 1px !important" /><input type="radio" name="GoogleColorPalette" value="#f3f3f3" id="light-grey-3" class="google-color-palette" style="background-color:#f3f3f3;color:#000000;border:#d9d9d9 solid 1px !important" /><input type="radio" name="GoogleColorPalette" value="#ffffff" id="white" class="google-color-palette" style="background-color:#ffffff;color:#000000;border:#d9d9d9 solid 1px !important" /><br /><input type="radio" name="GoogleColorPalette" value="#980000" id="red-berry" class="google-color-palette" style="background-color:#980000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#ff0000" id="red" class="google-color-palette" style="background-color:#ff0000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#ff9900" id="orange" class="google-color-palette" style="background-color:#ff9900;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ffff00" id="yellow" class="google-color-palette" style="background-color:#ffff00;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#00ff00" id="green" class="google-color-palette" style="background-color:#00ff00;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#00ffff" id="cyan" class="google-color-palette" style="background-color:#00ffff;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#4a86e8" id="cornflower-blue" class="google-color-palette" style="background-color:#4a86e8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#0000ff" id="blue" class="google-color-palette" style="background-color:#0000ff;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#9900ff" id="purple" class="google-color-palette" style="background-color:#9900ff;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#ff00ff" id="magenta" class="google-color-palette" style="background-color:#ff00ff;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#e6b8af" id="light-red-berry-3" class="google-color-palette" style="background-color:#e6b8af;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#f4cccc" id="light-red-3" class="google-color-palette" style="background-color:#f4cccc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#fce5cd" id="light-orange-3" class="google-color-palette" style="background-color:#fce5cd;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#fff2cc" id="light-yellow-3" class="google-color-palette" style="background-color:#fff2cc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d9ead3" id="light-green-3" class="google-color-palette" style="background-color:#d9ead3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d0e0e3" id="light-cyan-3" class="google-color-palette" style="background-color:#d0e0e3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#c9daf8" id="light-cornflower-blue-3" class="google-color-palette" style="background-color:#c9daf8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#cfe2f3" id="light-blue-3" class="google-color-palette" style="background-color:#cfe2f3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d9d2e9" id="light-purple-3" class="google-color-palette" style="background-color:#d9d2e9;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ead1dc" id="light-magenta-3" class="google-color-palette" style="background-color:#ead1dc;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#dd7e6b" id="light-red-berry-2" class="google-color-palette" style="background-color:#dd7e6b;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ea9999" id="light-red-2" class="google-color-palette" style="background-color:#ea9999;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#f9cb9c" id="light-orange-2" class="google-color-palette" style="background-color:#f9cb9c;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ffe599" id="light-yellow-2" class="google-color-palette" style="background-color:#ffe599;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#b6d7a8" id="light-green-2" class="google-color-palette" style="background-color:#b6d7a8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#a2c4c9" id="light-cyan-2" class="google-color-palette" style="background-color:#a2c4c9;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#a4c2f4" id="light-cornflower-blue-2" class="google-color-palette" style="background-color:#a4c2f4;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#9fc5e8" id="light-blue-2" class="google-color-palette" style="background-color:#9fc5e8;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#b4a7d6" id="light-purple-2" class="google-color-palette" style="background-color:#b4a7d6;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#d5a6bd" id="light-magenta-2" class="google-color-palette" style="background-color:#d5a6bd;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#cc4125" id="light-red-berry-1" class="google-color-palette" style="background-color:#cc4125;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#e06666" id="light-red-1" class="google-color-palette" style="background-color:#e06666;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#f6b26b" id="light-orange-1" class="google-color-palette" style="background-color:#f6b26b;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#ffd966" id="light-yellow-1" class="google-color-palette" style="background-color:#ffd966;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#93c47d" id="light-green-1" class="google-color-palette" style="background-color:#93c47d;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#76a5af" id="light-cyan-1" class="google-color-palette" style="background-color:#76a5af;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#6d9eeb" id="light-cornflower-blue-1" class="google-color-palette" style="background-color:#6d9eeb;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#6fa8dc" id="light-blue-1" class="google-color-palette" style="background-color:#6fa8dc;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#8e7cc3" id="light-purple-1" class="google-color-palette" style="background-color:#8e7cc3;color:#000000" /><input type="radio" name="GoogleColorPalette" value="#c27ba0" id="light-magenta-1" class="google-color-palette" style="background-color:#c27ba0;color:#000000" /><br /><input type="radio" name="GoogleColorPalette" value="#a61c00" id="dark-red-berry-1" class="google-color-palette" style="background-color:#a61c00;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#cc0000" id="dark-red-1" class="google-color-palette" style="background-color:#cc0000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#e69138" id="dark-orange-1" class="google-color-palette" style="background-color:#e69138;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#f1c232" id="dark-yellow-1" class="google-color-palette" style="background-color:#f1c232;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#6aa84f" id="dark-green-1" class="google-color-palette" style="background-color:#6aa84f;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#45818e" id="dark-cyan-1" class="google-color-palette" style="background-color:#45818e;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#3c78d8" id="dark-cornflower-blue-1" class="google-color-palette" style="background-color:#3c78d8;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#3d85c6" id="dark-blue-1" class="google-color-palette" style="background-color:#3d85c6;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#674ea7" id="dark-purple-1" class="google-color-palette" style="background-color:#674ea7;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#a64d79" id="dark-magenta-1" class="google-color-palette" style="background-color:#a64d79;color:#ffffff" /><br /><input type="radio" name="GoogleColorPalette" value="#85200c" id="dark-red-berry-2" class="google-color-palette" style="background-color:#85200c;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#990000" id="dark-red-2" class="google-color-palette" style="background-color:#990000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#b45f06" id="dark-orange-2" class="google-color-palette" style="background-color:#b45f06;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#bf9000" id="dark-yellow-2" class="google-color-palette" style="background-color:#bf9000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#38761d" id="dark-green-2" class="google-color-palette" style="background-color:#38761d;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#134f5c" id="dark-cyan-2" class="google-color-palette" style="background-color:#134f5c;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#1155cc" id="dark-cornflower-blue-2" class="google-color-palette" style="background-color:#1155cc;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#0b5394" id="dark-blue-2" class="google-color-palette" style="background-color:#0b5394;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#351c75" id="dark-purple-2" class="google-color-palette" style="background-color:#351c75;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#741b47" id="dark-magenta-2" class="google-color-palette" style="background-color:#741b47;color:#ffffff" /><br /><input type="radio" name="GoogleColorPalette" value="#5b0f00" id="dark-red-berry-3" class="google-color-palette" style="background-color:#5b0f00;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#660000" id="dark-red-3" class="google-color-palette" style="background-color:#660000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#783f04" id="dark-orange-3" class="google-color-palette" style="background-color:#783f04;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#7f6000" id="dark-yellow-3" class="google-color-palette" style="background-color:#7f6000;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#274e13" id="dark-green-3" class="google-color-palette" style="background-color:#274e13;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#0c343d" id="dark-cyan-3" class="google-color-palette" style="background-color:#0c343d;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#1c4587" id="dark-cornflower-blue-3" class="google-color-palette" style="background-color:#1c4587;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#073763" id="dark-blue-3" class="google-color-palette" style="background-color:#073763;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#20124d" id="dark-purple-3" class="google-color-palette" style="background-color:#20124d;color:#ffffff" /><input type="radio" name="GoogleColorPalette" value="#4c1130" id="dark-magenta-3" class="google-color-palette" style="background-color:#4c1130;color:#ffffff" /><br /> <div class="color-pick"> <input type="color" id="col-picker" tabindex="-1" class="hidden-col-picker" /> <input type="button" id="col-pick-button" class="col-pick-button" value="Custom" /> </div> </div> <script> //#### Color selection expander #### function expander() { var arrow = document.getElementById("color-result-expand").innerText; if (arrow === "ᐯ") { document.getElementById("color-picker").style.display = "block"; document.getElementById("color-result-expand").innerHTML = "<strong>ᐱ</strong>"; } else { document.getElementById("color-picker").style.display = "none"; document.getElementById("color-result-expand").innerHTML = "<strong>ᐯ</strong>"; } } //#### Color Picker #### //When "Custom" is clicked is goes to the color menu. document .getElementById("col-pick-button") .addEventListener("click", function() { document.getElementById("col-picker").focus(); document.getElementById("col-picker").value = "#ffffff"; document.getElementById("col-picker").click(); }); //Check when color picker is used. document .getElementById("col-picker") .addEventListener("change", watchColorPicker); function watchColorPicker(e) { //Uncheck radio button. let checked = document.querySelector('input[name="GoogleColorPalette"]:checked') !== null; if (checked) { document.querySelector( 'input[name="GoogleColorPalette"]:checked' ).checked = false; } //Get the selected hex color let col_value = event.target.value; //Determin if dark or light color // https://codepen.io/andreaswik/pen/YjJqpK function blackOrWhite(hexcolor) { hexcolor = hexcolor.replace("#", ""); var r = parseInt(hexcolor.substr(0, 2), 16); var g = parseInt(hexcolor.substr(2, 2), 16); var b = parseInt(hexcolor.substr(4, 2), 16); var yiq = (r * 299 + g * 587 + b * 114) / 1000; return yiq >= 128 ? "black" : "white"; } let text_col = blackOrWhite(col_value); //Change color colorSelected(col_value, "Custom", text_col); } //#### Color Radio Buttons #### //Get the value of the checked button. function radioClick() { let value = this.value; let id = this.id; let text_col = this.style.color; colorSelected(value, id, text_col); } document .querySelectorAll("input.google-color-palette") .forEach(button => { button.addEventListener("click", radioClick); }); //#### Dislplay Color #### function colorSelected(value, id, text_col) { //get the id document.getElementById("color-result-hex").textContent = value; document.getElementById("color-result-name").textContent = id; const result = document.getElementById("color-result"); result.style.color = text_col; result.style.backgroundColor = value; //add color val } </script> |
Example Usage
The following example will walk you through how to incorporate the code into your own project. I will be using the CSS package for Add-on’s-friendly version of the script.
You should be able to access the full example by going to the Google Sheet below:
Go to File > Make a copy. Then got to Extensions > Apps Script (it should automatically take you to: “Color palette selection” project) and run the onOpen() function in the code.gs for the first time to go through all the permissions.
In the example, we will be creating a menu item in our Add-on menu dropdown. The user will be able to select a sidebar or dialogue box example off the Colour Picker sub-menu.
Selecting one of these options will display a window with the Colour Picker. The user can then make a colour choice and it will be displayed in cell A1 in the ‘test’ Google Sheet tab.
We will be using 3 files:
- Code.gs: the core file to run the menu items, sidebars and dialogues, along with receiving data from the HTML files.
- index.html: The basic main HTML display file. It contains a header, the included Colour Picker file picker.html and a submit button.
- picker.html: The main Colour Picker file ( I won’t be walking through this).
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!DOCTYPE html> <html> <head> <base target="_top"> <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> </head> <body> <h1 style="padding: 10px;">Color Picker Test</h1> <div id="color" style="margin:0 auto; width:250px"> <?!= include('picker') ?> </div> <div style="padding: 10px;"> <input type="submit" onclick="getData()"/> </div> </body> <script> function getData(){ var hex_col = document.getElementById("color-result-hex").innerText; var name_col = document.getElementById("color-result-name").innerText; var txt_col = document.getElementById("color-result").style.color; var color = [hex_col, name_col, txt_col]; google.script.run.withSuccessHandler().receiveData(color); }; </script> </html> |
This file does not have too much extra in it. However, there are some important things to take note of:
Google’s CSS package for Add-ons
On line 5 you can see that I have added CSS Google Add-ons package.
Include picker
On line 10, you can see the <?!= include('picker') ?>
. This is a custom function that imports the picker.html, out Colour Picker file, into the index.html file.
getdata function
The getData()
function on lines 18-25 gets the three data elements from the selected colour; the hex code, the Google colour name, and the text colour.
It this adds these three colours to an array on line 23.
Finally, it sends that array to the function receiveData
in the Code.gs file to be used in Google Apps Script.
Code.gs
Note! Use the pop-out capability for the script below so you can follow the code as it is being described.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
//Sets up menu when spreadsheet is opened. function onOpen() { var UI = SpreadsheetApp.getUi(); UI.createAddonMenu() .addItem("Show sidebar","sidebar") .addItem("Show prompt","prompt") .addToUi(); }; //Custom Sidebar function sidebar(){ var ui = SpreadsheetApp.getUi(); var html = HtmlService .createTemplateFromFile("index") .evaluate(); html.setTitle("Standard Color Test"); ui.showSidebar(html); }; //Custom dialog function prompt(){ var ui = SpreadsheetApp.getUi(); var html = HtmlService .createTemplateFromFile("index") .evaluate(); ui.showModalDialog(html, "Standard Color Test"); }; // Creates an import or include function so files can be added // inside the main index. function include(filename){ return HtmlService.createHtmlOutputFromFile(filename) .getContent(); }; //Receives data from prompt or sidebar and updates the first cell. function receiveData(data){ if(data[0].indexOf("Select") !== -1){ SpreadsheetApp.getUi().alert("You need to select a color!"); return; }; var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("test"); var cell = sheet.getRange("A1"); cell.setValue(data[1]) .setBackground(data[0]) .setFontColor(data[2]) .setFontSize(16); }; |
onopen()
The onOpen()
is a simple trigger function that runs when a Google Sheet, Doc or Slide is opened.
When the file opens we want to create and Add-ons (now Extensions) menu item for our sidebar and dialogue prompt item to display our Colour Picker in.
When Show sidebar is clicked, the sidebar()
function is called. Alternatively, if Show prompt is clicked, the prompt()
function for the custom dialogue box is called.
sidebar() and prompt()
Both the sidebar()
and prompt()
functions are similar. Both call the user interface method with getUi(). They then invoke HtmlService class to create a template from a file.
Make sure you use the createTemplateFromFile()
method. This method allows us to add Google Apps Script code right in the HTML file using <? code in here ?>
. This will be useful especially when we use the include
function later.
In the final line of each function, you can see that we call showSidebar(html)
to show our HTML file in our sidebar and showModalDialog(html, "Standard Color Test")
to show our HTML in our custom popup dialogue prompt.
Sidebar: Colour Picker
Dialogue Box Prompt: Colour Picker
include(filename)
The include(filename)
function is called inside the index.html file to add the picker.html file to the main index.html file.
The function takes an HTML file name. It gets the output content from the picker.html file using HtmlService.createHtmlOutputFromFile(filename) .getContent();
and adds it to the index.html file.
Another example of this can be found in this tutorial:
receiveData(data)
The receiveData(data)
function takes the data from the Colour Picker retrieved in the getData()
function inside the index.html file.
Remember this data variable is an array containing:
1 |
var data = ["<em>hex color</em>","<em>Google color name</em>","<em>text color</em>"] |
On line 42-45, we quickly check to see if the user has a selected colour by checking if the zeroeth value of the array contains the word “Select”. This is the stand-in word when the Colour Picker is first loaded.
If no selection has been made, then we send an alert message to the user.
Otherwise, we update cell A1 in the ‘test’ Google Sheet tab to the selected colour (data[0]
), give it the background colour of the selected colour (data[0]
), change the text colour to the colour of the text (data[2]
) and of course, add the Google name of the text (data[1]
).
Note that when we use a custom colour, the name is just “Custom”. There is no Google Name for custom colours.
The result from receiveData(data)
Conclusion
Hopefully, the details and example are enough for you to get started on your own project. If you think you need more information or have a question feel free to comment below.
My aim was to keep the Colour Picker in a similar style to Google’s so that the user had a more intuitive and seamless experience.
I hope you can make use of this little widget thingy and would love to hear how you used it in your own projects.
All the files for this project are completely free to use for commercial and non-commercial use.
Oh and P.S. I made an earlier post detailing the Hexadecimal colour codes and names for the Standard Google Palette. You can check it out here:
Google Apps Script: Hexadecimal Color Codes for Google Docs, Sheets and Slides Standard Palette
Happy coding!
Need help with Google Workspace development?
Go something to solve bigger than Chat GPT?
I can help you with all of your Google Workspace development needs, from custom app development to integrations and security. I have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.
Schedule a free consultation today to discuss your needs and get started or learn more about our services here.
~Yagi
I love this! Thank you. I have been trying to modify the script to fit my needs, but I can not get the custom color picker to work properly. Even on the example sheet it does not seem to display properly. Any ideas as to why?
Hi Quinn,
Thanks for the kind words.
I just ran a quick check on the codes and it seems to be running okay. Phew! That takes that out of the equation.
My best guess is that there might be some CSS that is interfering with the display of the code. I wrote two sets of CSS on to run with Googles recommended CSS package and one without.
Hopefully switching between one of those will help. Otherwise, it might be another CSS conflict.
You’re welcome to share your code in the comments below and I or one of our readers might have a fix for you.
Cheers,
Yagi
First thanks for this awesome work and code, AMAZING!! but…sorry…I must confirm that it is not working in the exemple – the custom button does not work and I have tried to switch to the older picker.html without the CSS package, in your exemple but in vain…
I found a fix!
go to the picker.html and replace left : -1000px by left:0 in the class “.hidden-col-picker” – done.
Great to hear, Remi.
how to implemet this in google form, i want to create some Graphic Design Briefing Form that have color picker for my client to choose one or more color they want.
Hi arifination,
You won’t be able to do that with a Google Form. I would recommend that you create form items in a Google Apps Script Web App. I have a few tutorials on this topic that may help you:
Google Apps Script: How to create a basic interactive interface with Web Apps
Creating an embedded interactive Chain Story app with Google Apps Script and Google Sheets