Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdn.jsdelivr.net/npm/pizzly-js@0.2.7/dist/index.umd.min.js"></script>

<main>
  <header>
    <button data-save-to-gsheet>
      <img width="18" height="18" src="https://cdn.glitch.com/34c68bfe-d0b3-427c-b5a7-0dc0980ab438%2Fgoogle-sheets.svg?v=1592381742810" />
      <span>Save to Google Sheets</span>
    </button>
    <a href="#" class="spreadsheet-link" style="visibility: hidden;" target="_blank">See results ↗</a>
  </header>
  <table>
    <thead>
      <tr>
        <th></th>
        <th>Name</th>
        <th>Native</th>
        <th>Weigh</th>
        <th>Population</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th><span>1</span></th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/American_black_bear">American black bear</a>
        </td>
        <td>North America</td>
        <td>57–250 kg</td>
        <td>396000</td>
      </tr>
      <tr>
        <th>2</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Asian_black_bear">Asian black bear</a>
        </td>
        <td>Southern China</td>
        <td>60–200 kg</td>
        <td>28000</td>
      </tr>
      <tr>
        <th>3</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Brown_bear">Brown bear</a>
        </td>
        <td>
          <div>Eurasia and North America</div>
        </td>
        <td>180 kg</td>
        <td>200000</td>
      </tr>
      <tr>
        <th>4</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Giant_panda">Giant panda</a>
        </td>
        <td>South central China</td>
        <td>70-160 kg</td>
        <td>2000</td>
      </tr>
      <tr>
        <th>5</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Sloth_bear">Sloth bear</a>
        </td>
        <td>India</td>
        <td>55-105 kg</td>
        <td>20000</td>
      </tr>
      <tr>
        <th>6</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Sun_bear">Sun bear</a>
        </td>
        <td>Southeast Asia</td>
        <td>55–143 pounds</td>
        <td>N/A</td>
      </tr>
      <tr>
        <th>7</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Polar_bear">Polar bear</a>
        </td>
        <td>Artic Circle</td>
        <td>350–700 kg</td>
        <td>22000</td>
      </tr>
      <tr>
        <th>8</th>
        <td>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Spectacled_bear">Spectacled bear</a>
        </td>
        <td>South America</td>
        <td>100-200 kg</td>
        <td>N/A</td>
      </tr>
    </tbody>
  </table>
  <p>Made with <a href="https://github.com/Bearer/Pizzly">Pizzly</a> by a group of bears 🐻</p>
</main>
              
            
!

CSS

              
                html,
body {
  font-family: -apple-system, BlinkMacSystemFont, segoe ui, roboto, oxygen,
    ubuntu, cantarell, fira sans, droid sans, helvetica neue, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: #5a4296;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

main {
  max-width: 650px;
  margin: 1rem auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

table thead tr {
  font-style: normal;
  font-weight: normal;
  font-size: 11px;
  line-height: 14px;
  text-transform: uppercase;
  color: #8b7eaf;
  border-bottom: 1px solid #e1dded;
}

table thead tr th {
  padding: 9px 0;
}

table tbody tr th:first-child {
  text-align: center;
  width: 48px;
  height: 24px;
  position: relative;
  font-weight: 400;
}

table tbody tr td {
  padding: 15px 0;
}

table tbody tr td:nth-child(2) {
  font-weight: bold;
}

a {
  text-decoration: none;
  color: inherit;
}

a:hover {
  text-decoration: underline;
}

header button,
header a {
  font-weight: bold;
  font-size: 1.1rem;
  padding: 0.9rem 20px;
  cursor: pointer;
  border-radius: 3px;
  box-sizing: border-box;
  display: inline-block;
}

header button {
  background: #eef9ef;
  border: 1px solid #43a047;
  color: #2e7d32;
  margin-right: 1rem;
}

header button img {
  float: left;
  margin-right: 0.5rem;
}

header a {
  border: 1px solid #345bff;
}

header {
  display: flex;
  margin: 1.5rem 0;
}

              
            
!

JS

              
                const saveToGSheet = {
  pizzly: null,
  authId: null,
  spreadsheetId: null,
  saving: false,

  /**
   * Initializing all buttons and Pizzly
   */

  initialize: function (key, host) {
    if (typeof Pizzly !== "function") {
      throw new Error("Unable to initialize. Missing Pizzly JS library");
    }

    // Initialize Pizzly with demo instance
    this.pizzly = new Pizzly(key, host);

    // Add click event to all "save-to-gsheet" elements on the page
    document.querySelectorAll("[data-save-to-gsheet]").forEach((elt) => {
      elt.addEventListener("click", (e) => {
        this.save(elt);
      });
    });
  },

  /**
   * Handle save event (user click on a button)
   */

  save: async function (buttonElt) {
    if (this.saving) {
      return;
    }

    // Lock the saving process
    this.saving = true;
    buttonElt.innerText = "Saving...";

    // Connect user (if not connected already)
    if (!this.authId) {
      await this.connect();
    }

    // Create spreadsheet
    await this.createNewSpreadsheet();

    if (this.authId && this.spreadsheetId) {
      const data = this.fetchDataFromDOM(buttonElt);
      await this.saveDataToGoogleSheet(data);

      buttonElt.innerText = "Saved! 🎉";

      // Show the spreadsheet link
      const link = document.querySelector(".spreadsheet-link");
      link.href =
        "https://docs.google.com/spreadsheets/d/" +
        this.spreadsheetId +
        "/edit#gid=0";
      link.style.visibility = "visible";
    }

    // Unlock the saving process
    this.saving = false;
  },

  /**
   * Trigger the connection to the Google Sheets API
   */

  connect: function () {
    return this.pizzly
      .connect("google-sheets")
      .then(({ authId }) => {
        // The authentication was successful
        console.log(`Connected! The authId is: ${authId}`);

        // Save the authId for later use
        this.authId = authId;
      })
      .catch((error) => {
        // The authentication failed
        console.error(error);
      });
  },

  /**
   * Save a new spreadsheet on the user's Google Drive
   */

  createNewSpreadsheet: function () {
    if (!this.authId) {
      return;
    }

    // Use Pizzly proxy mode to request the Google Sheets API.
    // The request will create a new spreadsheet and return its ID.
    return this.pizzly
      .integration("google-sheets")
      .auth(this.authId)
      .post("/")
      .then((response) => response.json())
      .then((data) => {
        // Successfully created spreadshet!
        console.log("Spreadsheet created!", data);

        // Save the spreadsheetId for later use
        this.spreadsheetId = data.spreadsheetId;
      })
      .catch(console.error);
  },

  /**
   * Fetch data from the nearest table to the button
   */

  fetchDataFromDOM: function (buttonElt) {
    if (!buttonElt) {
      return;
    }

    let tableElt;
    let parentElt = buttonElt;

    // Find the neareast table
    while (parentElt && !tableElt) {
      parentElt = parentElt.parentNode;
      tableElt = parentElt.querySelector("table");
    }

    if (!tableElt) {
      return;
    }

    const data = [];

    // Fetch data from the table
    tableElt.querySelectorAll("tr").forEach(function (row) {
      const dataRow = [];

      row.querySelectorAll("th").forEach(function (cell) {
        dataRow.push(cell.innerText);
      });

      row.querySelectorAll("td").forEach(function (cell) {
        dataRow.push(cell.innerText);
      });

      data.push(dataRow);
    });

    return data;
  },

  /**
   * Save the provided data into the provided spreadsheet
   */

  saveDataToGoogleSheet: function (data) {
    if (!data || !this.authId || !this.spreadsheetId) {
      return;
    }

    data.push([]);
    data.push(["🐻 Saved with", "https://github.com/Bearer/Pizzly"]);

    return this.pizzly
      .integration("google-sheets")
      .auth(this.authId)
      .post(
        "/" +
          this.spreadsheetId +
          "/values/A1:append?valueInputOption=RAW",
        {
          body: JSON.stringify({ values: data })
        }
      )
      .then((response) => response.json())
      .then((body) => {
        console.log("Data saved!", body);
      })
      .catch(console.error);
  }
};

const demoKey = "pope8Qy8qfYyppnHRMgLMpQ8MuEUKDGeyhfGCj";
const demoInstance = "https://pushtogsheet.herokuapp.com";
saveToGSheet.initialize(demoKey, demoInstance);

              
            
!
999px

Console