How to automate Force Sync?

This guide explains how to force sync your Product Sheet with your Store.link website when manual syncing isn't feasible.

Note
This guide assumes familiarity with Google Apps Script and basic coding. If you're not comfortable with these, please consult a developer.

Why Use Force Sync?

Store.link typically synchronizes your Product Sheet with your website automatically. This works well if you're manually entering products into the sheet. However, if you're using third-party add-ons, custom scripts, or syncing from another sheet with the IMPORTRANGE formula, you might not be directly modifying the Product Sheet.

In these cases, automatic syncing may fail, and you'll need to implement an automated force sync. A manual force sync is always available.

Implementing Force Sync

To automate force syncing, add the following code to the Google Sheet linked to your Store.link website, which contains your "Products" and "Orders" sheets:

function decrementDeveloperMetadata() {
  // Get the active spreadsheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  
  // Use the key "sl-version"
  var key = "sl-version";
  
  // Get all metadata with the specified key
  var metadata = spreadsheet.getDeveloperMetadata().filter(function(item) {
    return item.getKey() === key;
  });
  
  if (metadata.length === 0) {
    // If no metadata with the given key exists, create a new one with value 0
    spreadsheet.addDeveloperMetadata(key, "0");
    Logger.log("Created new metadata with key '" + key + "' and value 0");
  } else {
    // If metadata exists, decrement its value by 1
    var currentValue = parseInt(metadata[0].getValue()) || 0;
    var newValue = Math.max(currentValue - 1, 0); // Ensure the value doesn't go below 0
    metadata[0].setValue(newValue.toString());
    Logger.log("Decremented metadata with key '" + key + "' to " + newValue);
  }
}

Auto update works by checking if the sheet version is the latest one. We use the the key sl-version for tracking sheet version.

When you use custom Apps Scripts to modify the sheet content, the sheet version may not be updated. Using the above function decrementDeveloperMetadata() we can change the version number.

Every time your Online Store has a user visit, we check if there has been any update for the sheet version using the same key, sl-version. By calling the function decrementDeveloperMetadata() inside your custom Apps Script function, the sheet version will be auto updated. This change in sheet version will trigger a force sync by itself.



Still need help?

Contact us

FAQ