Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support version 72+ #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions Google sheets content sync.sketchplugin/Contents/Sketch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var appVersion, plugin

// Setup variables based on the context
function setup(context) {

selection = context.selection
doc = context.document
scriptPath = context.scriptPath
Expand All @@ -19,7 +20,7 @@ function setup(context) {

iconImage = NSImage.alloc().initByReferencingFile(context.plugin.urlForResourceNamed("icon.png").path())

appVersion = MSApplicationMetadata.metadata().appVersion
appVersion = BCSketchInfo.shared().metadata().appVersion

docData = context.document.documentData()
command = context.command;
Expand Down Expand Up @@ -75,18 +76,18 @@ function syncContent() {
if (!sheetId) {
var alert = NSAlert.alloc().init()
alert.setIcon(iconImage)
alert.setMessageText("Invalid Google Sheet URL")
alert.setInformativeText("The URL you entered wasn't valid")
alert.addButtonWithTitle("Ok")
alert.setMessageText("Invalid Google Sheet URL")
alert.setInformativeText("The URL you entered wasn't valid")
alert.addButtonWithTitle("Ok")
return alert.runModal()

} else if (sheetId == "e") {

var alert = NSAlert.alloc().init()
alert.setIcon(iconImage)
alert.setMessageText("Invalid Google Sheet URL")
alert.setInformativeText("It looks like you've used the URL generated on the 'Publish to the web' screen. \n\nWhilst it is important that you do 'publish' the spreadsheet to the web, you need to use the URL for the spreadsheet that is in the browser address bar instead.")
alert.addButtonWithTitle("Ok")
alert.setMessageText("Invalid Google Sheet URL")
alert.setInformativeText("It looks like you've used the URL generated on the 'Publish to the web' screen. \n\nWhilst it is important that you do 'publish' the spreadsheet to the web, you need to use the URL for the spreadsheet that is in the browser address bar instead.")
alert.addButtonWithTitle("Ok")
return alert.runModal()
}

Expand All @@ -97,7 +98,7 @@ function syncContent() {
return

// Update the values for each page
doc.pages().forEach(function(page) {
doc.pages().forEach(function (page) {

var sheetTitle = valueFromName(page.name())
var pageValues = valuesForSheet(sheetTitle)
Expand All @@ -108,14 +109,14 @@ function syncContent() {
pageValues = firstSheet.values
}

page.children().forEach(function(child) {
page.children().forEach(function (child) {
if (child.isMemberOfClass(MSSymbolInstance)) {

// Store new overrides that need to be made
var overrides = {}


child.symbolMaster().children().forEach(function(symbolLayer) {
child.symbolMaster().children().forEach(function (symbolLayer) {
// Ignore layers that are not text layers or shapes
// Only include layers that have a '#' in the name
if (!(symbolLayer.isMemberOfClass(MSTextLayer) || canLayerHaveFill(symbolLayer)) || symbolLayer.name().indexOf('#') < 0) {
Expand All @@ -127,13 +128,13 @@ function syncContent() {
var existingOverrides = child.overrides()
if (existingOverrides == null) {
// no overrides exist, add one
child.overrides = { objectID : "overrideText"}
child.overrides = { objectID: "overrideText" }
existingOverrides = child.overrides()
}

// get the existing overrides and create a mutable copy
var mutableOverrides = NSMutableDictionary.dictionaryWithDictionary(existingOverrides)
mutableOverrides.setObject_forKey(NSMutableDictionary.dictionaryWithDictionary(existingOverrides.objectForKey(0)),0)
mutableOverrides.setObject_forKey(NSMutableDictionary.dictionaryWithDictionary(existingOverrides.objectForKey(0)), 0)

var nameFormat = formatName(symbolLayer.name())
var childName = nameFormat.lookupName
Expand Down Expand Up @@ -161,7 +162,7 @@ function syncContent() {
// Set the value of the text layer accordingly
// Based on if it was given an index, otherwise return the first one
var value = (values.length >= index) ? values[index - 1] : ''
var textValue = (value == '' ? 'N/A' : value)
var textValue = value || ''

if (symbolLayer.isMemberOfClass(MSTextLayer)) {
mutableOverrides.setObject_forKey(textValue, objectID)
Expand Down Expand Up @@ -205,7 +206,7 @@ function syncContent() {
// Set the value of the text layer accordingly
// Based on if it was given an index, otherwise return the first one
var value = (values.length >= index) ? values[index - 1] : ''
var textValue = (value == '' ? 'N/A' : value)
var textValue = value || ''

if (child.isMemberOfClass(MSTextLayer)) {
child.setStringValue(textValue)
Expand Down Expand Up @@ -264,7 +265,7 @@ function getImageDataFromURL(url) {

var image

if (!data){
if (!data) {
print('Error fetching image')
doc.showMessage("Error in fetching image URL.")
image = NSImage.alloc().initByReferencingFile(plugin.urlForResourceNamed("Placeholder.png").path())
Expand All @@ -274,7 +275,7 @@ function getImageDataFromURL(url) {


if (isCurrentVersionBefore('47')) {
return MSImageData.alloc().initWithImage_convertColorSpace(image, false)
return MSImageData.alloc().initWithImage_convertColorSpace(image, false)
}

return MSImageData.alloc().initWithImage(image)
Expand Down Expand Up @@ -359,7 +360,7 @@ function valuesForSheet(sheetName) {
return null
}

var sheet = sheetValues.find(function(sheet) {
var sheet = sheetValues.find(function (sheet) {
return sheet.title.replace(/\s/g, '').toLowerCase() == sheetName.replace(/\s/g, '').toLowerCase()
})

Expand Down Expand Up @@ -400,7 +401,7 @@ function fetchValuesForPage(sheetID, pageNumber) {
try {
var data = JSON.parse(dataString)
return parseData(data)
} catch(e) {
} catch (e) {
doc.showMessage("Failed to process the document data correctly")
return null
}
Expand All @@ -410,10 +411,10 @@ function fetchValuesForPage(sheetID, pageNumber) {
function parseData(data) {
var values = {}

data.feed.entry.forEach(function(entry) {
Object.keys(entry).filter(function(key) {
data.feed.entry.forEach(function (entry) {
Object.keys(entry).filter(function (key) {
return key.indexOf('gsx$') == 0
}).forEach(function(key) {
}).forEach(function (key) {
var newKey = key.substring(4)
if (!(values.hasOwnProperty(newKey))) {
values[newKey] = []
Expand All @@ -439,9 +440,9 @@ function compareVersion(a, b) {
a = (a + '').replace(re, '').split('.');
b = (b + '').replace(re, '').split('.');
len = Math.min(a.length, b.length);
for( i = 0; i < len; i++ ) {
for (i = 0; i < len; i++) {
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
if( cmp !== 0 ) {
if (cmp !== 0) {
return cmp;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Sync content within a Google Sheets document, to text layers — based on their names",
"author": "David Williames",
"homepage": "https://github.com/DWilliames/Google-sheets-content-sync-sketch-plugin",
"version": "1.3",
"version": "1.4",
"identifier": "com.davidwilliames.sketch-plugins.google-sheets-content-sync",
"compatibleVersion": 39,
"bundleVersion": 1,
Expand Down