How to Make a Sankey Diagram in Google Sheets with ChatGPT

Cody Schneider7 min read

Google Sheets doesn't have a built-in option to create a Sankey diagram, but that doesn't mean you can't make one. This article will show you exactly how to visualize data flows by using ChatGPT to write a custom script that builds a Sankey diagram right inside your spreadsheet. We’ll walk through every step, from organizing your data to generating and running the code.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What is a Sankey Diagram and When Should You Use One?

A Sankey diagram is a type of flow chart where the width of the arrows or bands is proportional to the quantity of the flow. In simple terms, it's a powerful way to visualize how something moves from one stage to another. The thicker the band, the larger the amount.

Unlike a standard pie chart that just shows percentages or a bar chart that compares values, a Sankey shows the journey. It’s perfect for answering questions that involve "how much goes from here to there?"

Here are a few practical examples where a Sankey diagram shines:

  • Website Traffic Analysis: See how visitors move through your site. You can track them from a source (like Google, Facebook, or a newsletter) to a landing page, then to a pricing page, and finally to a sign-up confirmation. This helps you identify popular user paths and where people drop off.
  • Marketing Funnel Visualization: Map out the customer journey. You can show the flow from initial ad impressions to clicks, on to leads generated, and finally to closed sales. This gives you a clear picture of conversion rates at each stage of your funnel.
  • Budget Allocation: Visualize how a company's total budget is distributed across different departments (Marketing, Sales, R&D) and then broken down further into specific projects or expenses within those departments.
  • E-commerce Sales Flow: Track how customers find your products. You could map the flow from a marketing channel (e.g., Instagram Ad, SEO) to a product category, and then to the actual purchase.

Any time you need to show a many-to-many relationship with flowing quantities, a Sankey diagram is one of the clearest and most intuitive ways to do it.

Prepare Your Data for the Sankey Diagram

Before we can build the chart, we need to structure our data correctly in Google Sheets. The Sankey diagram script requires a very specific format: a three-column table consisting of a Source, a Target, and a Value.

  • Source: The starting point of the flow.
  • Target: The destination of the flow.
  • Value: The quantity or amount of the flow. This must be a number.

Each row represents a single path from a source to a target. It’s important to keep your labels consistent. For instance, if you use "Social Media" as a Source, make sure you don't accidentally write "Social" in another row, as the chart will treat them as two different nodes.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Example: Mapping Website Traffic

Let's use a marketing example. Imagine we want to visualize how people get to our website and what they do once they arrive. We’re tracking the flow from the acquisition channel to their first landing page.

Create a new sheet in your Google Sheets workbook and name it "Sankey Data". It's crucial you use this exact name, as our script will be programmed to look for it.

In this "Sankey Data" sheet, set up your data like this:

This data tells a story: Organic Search is our biggest driver to blog posts, Paid Search sends traffic directly to a dedicated landing page, and both paths ultimately lead to our Sign Up Page.

How to Create the Sankey Diagram with ChatGPT and Apps Script

Now that our data is ready, we'll use ChatGPT to generate the Google Apps Script code we need. This might sound technical, but you don't need to know how to code - you just need to know how to copy and paste.

Step 1: Open Google Apps Script

From your Google Sheet, go to the menu at the top and click on Extensions > Apps Script. This will open a new tab with the Google Apps Script editor. This is where we will put the code that creates our chart.

You can delete any default code that appears in the editor window.

Step 2: Prompt ChatGPT to Write the Script

Go to ChatGPT and use a clear, specific prompt. Providing detailed instructions helps the AI generate accurate, working code on the first try.

Here’s an effective prompt you can use:

“Write a Google Apps Script function called `createSankeyDiagram`. This function should:

ChatGPT will process this and give you a complete script.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Step 3: Copy and Paste the Code into Apps Script

Copy the entire block of code generated by ChatGPT and paste it into the Apps Script editor window you opened earlier.

The code should look something like this:

function createSankeyDiagram() { var ss = SpreadsheetApp.getActiveSpreadsheet(), var dataSheet = ss.getSheetByName("Sankey Data"), var chartSheet = ss.getSheetByName("Sankey Chart"),

// If the chart sheet doesn't exist, create it. if (!chartSheet) { chartSheet = ss.insertSheet("Sankey Chart"), } else { // If it exists, clear it of any old charts. chartSheet.getCharts().forEach(function(chart) { chartSheet.removeChart(chart), }), chartSheet.clear(), }

// Get data range, excluding the header row var dataRange = dataSheet.getRange(2, 1, dataSheet.getLastRow() - 1, 3), var data = dataRange.getValues(),

// Create a DataTable for the chart. var dataTable = Charts.newDataTable() .addColumn(Charts.ColumnType.STRING, "From") .addColumn(Charts.ColumnType.STRING, "To") .addColumn(Charts.ColumnType.NUMBER, "Weight"),

// Add the data to the DataTable data.forEach(function(row) { dataTable.addRow(row), }),

// Build the chart. var chartBuilder = Charts.newSankeyChart() .setDataTable(dataTable) .setOption('width', 800) .setOption('height', 600) .setOption('sankey.node.labels', { fontName: 'Arial', fontSize: 12, color: '#000', bold: true }) .setOption('sankey.link.colorMode', 'gradient') .setOption('sankey.node.labelPadding', 10),

var chart = chartBuilder.build(),

// Insert the chart into the sheet. chartSheet.insertImage(chart.getAs('image/png'), 1, 1),

SpreadsheetApp.setActiveSheet(chartSheet), }

Once you’ve pasted the code, click the floppy disk icon to save the project. You can give it a name like "Sankey Chart Generator".

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Step 4: Run the Script

Now it’s time to run the script and create your chart.

  1. Make sure the function name at the top of the editor, createSankeyDiagram, is selected in the dropdown menu.
  2. Click the "Run" button (it looks like a play icon).

The first time you run a script, Google will ask for your permission for the script to access your spreadsheet data. Click "Review permissions," choose your Google account, and click "Allow" on the next screen. This is a standard security step and you only need to do it once.

After giving permission, you might need to click "Run" one more time. The script will execute, and within a few seconds, it will create a new sheet named "Sankey Chart" and place your diagram inside it as an image.

You did it! You should now have a custom Sankey diagram in your Google Sheet, ready for your report or presentation.

Final Thoughts

As you can see, Google Sheets becomes a much more powerful tool when you combine it with the code-writing ability of an AI like ChatGPT. You can now generate custom visualizations like Sankey diagrams to tell a clearer, more compelling story with your data, without having to write a single line of code yourself.

While creating custom charts for a specific spreadsheet is a great start, the real challenge is keeping all of your reports updated, especially when your data lives across many different platforms. A lot of time gets wasted downloading CSVs and wrangling them into the right format. Our goal with Graphed was to eliminate that manual work. We automated the data connection process so you can bring together sources like Google Analytics, Shopify, and Facebook Ads, and then just ask for the report you want in plain English. It's about a permanent, live dashboard, instead of a picture in a spreadsheet.

Related Articles