google Apps Script API Shopify

google Apps Script API Shopify

strater_root
Visitor
3 0 0

I am using Google's Apps Script API to fetch product data from the Shopify API. However, the results are incomplete and incorrect. Can anyone guide me on how to properly use the code?

Replies 3 (3)

FrankyGiap
Shopify Partner
88 15 12

Hi @strater_root,

You can provide more details about usecase and error you met?

- Solved it? Hit Like and Accept solution or ❤️Buy Me Coffee❤️
- Fusion Workflow: Automate Your Store with Precision and Ease
strater_root
Visitor
3 0 0

const functions = require('@google-cloud/functions-framework');
const { google } = require('googleapis');
const axios = require('axios');
const { Parser } = require('json2csv');

const SHOPIFY_STORE = "xxxxxx";
const ACCESS_TOKEN = "xxxxxx"; // Shopify API Token
const API_VERSION = "2023-10";
const FOLDER_ID = "xxxxxxxx"; // Google Drive Folder ID

// Fetch products from Shopify
async function fetchShopifyProducts() {
const url = `https://${SHOPIFY_STORE}.myshopify.com/admin/api/${API_VERSION}/products.json?limit=250`;
const response = await axios.get(url, {
headers: { "X-Shopify-Access-Token": ACCESS_TOKEN }
});
return response.data.products;
}

// Convert JSON data to CSV format
function convertToCSV(products) {
const fields = [
"Handle", "Title", "Body (HTML)", "Vendor", "Product Category", "Type",
"Tags", "Published", "Option1 Name", "Option1 Value",
"Option2 Name", "Option2 Value", "Option3 Name", "Option3 Value",
"Variant SKU", "Variant Grams", "Variant Inventory Tracker",
"Variant Inventory Qty", "Variant Inventory Policy", "Variant Fulfillment Service",
"Variant Price", "Variant Compare At Price", "Variant Requires Shipping",
"Variant Taxable", "Variant Barcode", "Image Src", "Image Position",
"Image Alt Text", "Gift Card", "SEO Title", "SEO Description",
"Google Shopping / Google Product Category", "Google Shopping / Gender",
"Google Shopping / Age Group", "Google Shopping / MPN",
"Google Shopping / AdWords Grouping", "Google Shopping / AdWords Labels",
"Google Shopping / Condition", "Google Shopping / Custom Product",
"Google Shopping / Custom Label 0", "Google Shopping / Custom Label 1",
"Google Shopping / Custom Label 2", "Google Shopping / Custom Label 3",
"Google Shopping / Custom Label 4", "Variant Image", "Variant Weight Unit",
"Variant Tax Code", "Cost per item", "Price / International",
"Compare At Price / International", "Status"
];

const rows = [];

products.forEach(product => {
product.variants.forEach(variant => {
rows.push({
Handle: product.handle,
Title: product.title,
"Body (HTML)": product.body_html,
Vendor: product.vendor,
"Product Category": product.product_type,
Type: product.type,
Tags: Array.isArray(product.tags) ? product.tags.join(", ") : "",
Published: product.published_at,
"Option1 Name": product.options[0]?.name || "",
"Option1 Value": variant.option1 || "",
"Option2 Name": product.options[1]?.name || "",
"Option2 Value": variant.option2 || "",
"Option3 Name": product.options[2]?.name || "",
"Option3 Value": variant.option3 || "",
"Variant SKU": variant.sku,
"Variant Grams": variant.weight,
"Variant Inventory Tracker": variant.inventory_management,
"Variant Inventory Qty": variant.inventory_quantity,
"Variant Inventory Policy": variant.inventory_policy,
"Variant Fulfillment Service": variant.fulfillment_service,
"Variant Price": variant.price,
"Variant Compare At Price": variant.compare_at_price,
"Variant Requires Shipping": variant.requires_shipping,
"Variant Taxable": variant.taxable,
"Variant Barcode": variant.barcode,
"Image Src": product.images[0]?.src || "",
"Image Position": "1",
"Image Alt Text": "",
"Gift Card": "",
"SEO Title": product.title,
"SEO Description": product.body_html,
"Google Shopping / Google Product Category": "",
"Google Shopping / Gender": "",
"Google Shopping / Age Group": "",
"Google Shopping / MPN": "",
"Google Shopping / AdWords Grouping": "",
"Google Shopping / AdWords Labels": "",
"Google Shopping / Condition": "",
"Google Shopping / Custom Product": "",
"Google Shopping / Custom Label 0": "",
"Google Shopping / Custom Label 1": "",
"Google Shopping / Custom Label 2": "",
"Google Shopping / Custom Label 3": "",
"Google Shopping / Custom Label 4": "",
"Variant Image": "",
"Variant Weight Unit": "",
"Variant Tax Code": "",
"Cost per item": "",
"Price / International": "",
"Compare At Price / International": "",
Status: "active"
});
});
});

const parser = new Parser({ fields });
return parser.parse(rows);
}

// Upload CSV file to Google Drive
async function uploadToDrive(csvData) {
const auth = new google.auth.GoogleAuth({ scopes: ["https://www.googleapis.com/auth/drive"] });
const drive = google.drive({ version: "v3", auth });

const fileMetadata = { name: `Shopify_Products_${Date.now()}.csv`, parents: [FOLDER_ID] };
const media = { mimeType: "text/csv", body: csvData };

await drive.files.create({ resource: fileMetadata, media, fields: "id" });
}

// Cloud Function
functions.http("shopifyToDrive", async (req, res) => {
try {
const products = await fetchShopifyProducts();
const csvData = convertToCSV(products);
await uploadToDrive(csvData);
res.status(200).send(" Data successfully saved to Google Drive!");
} catch (error) {
res.status(500).send(` Error: ${error.message}`);
}
});

strater_root
Visitor
3 0 0

If the data in the exported file does not match the manual export from Shopify, here are some potential reasons and solutions: