kentico-ericd / kontent-google-sheets-add-on

Using this Google Sheets add-on, you can import new content items in Kentico Kontent, or update existing items. This can be handy when migrating content from an external database or CMS, or if your editors prefer to work in spreadsheets.
MIT License
4 stars 3 forks source link

Stack Overflow

Sheets Google Sheets Import

Installation

You can install this Google Workspace add-on here or directly from Google Sheets using this button in the sidebar:

Install

After installing, click the Kontent icon in the sidebar to load the add-on's home page. Locate the following API keys in Kontent, then add them in the addon's Project settings menu,

Preparing the Sheet

You may create a new Sheet or edit an existing one. The Generate Sheet menu of the add-on can also generate a new Sheet for you, along with the required headers, based on the project's content types.

If you have a local spreadsheet file, you can open it in Google Sheets by uploading it to Google Drive, then selecting Open with > Google Sheets.

Open in Sheets

Once the file is open in Sheets, you must re-save it as a Google Sheet file:

Save as Sheet

:warning: The name of the Sheet must match the code name of the content type in your project which will be used to create items

If you used the Generate Sheet menu to create the Sheet, the code name of the content type will be automatically generated. If you are creating the Sheet manually, you can find the content type code name in Kontent:

Content type codename

Setting the headers

The headers (first row) of your Sheet must contain the code names of the content type's elements. If you use the Generate Sheet menu of the add-on, these headers will be automatically generated. If adding headers manually, you can find the code names of the elements when editing the content type in https://app.kontent.ai.

Code names

In addition to the element code names, the header row can also contain these other headers:

:bulb: See the Locating and updating existing items section to read more about how existing items are located and how to update content item names and codenames.

The following is an example of what a Sheet named product might look like:

external_id sku name price date_offered
F4891FB5-5215-4795-8A6F-18A4F68394FD CO-ETH-YIRGACHEFFE Ethiopia Yirgacheffe (decaf) 3.5 2017-01-10 15:46:54.5576119
DD84A64C-F0BE-42AA-9F47-228ED6520D27 CO-ETH-YIRGACHEFFE-5-lb Ethiopia Yirgacheffe (decaf) (5 lb) 52 2017-01-10 16:02:44.6796146
031A9DE2-51F4-41F7-B2FE-5825FBAADD6C CO-ETH-YIRGACHEFFE-4-oz Ethiopia Yirgacheffe (decaf) (4 oz) 3.5 2017-01-10 16:02:44.6186085
FE991A97-3998-4FA0-9DF7-59A00622297B CO-ETH-YIRGACHEFFE-16-oz Ethiopia Yirgacheffe (decaf) (16 oz) 12 2017-01-10 16:02:44.5085975

Formatting Cell Values

To avoid errors in importing data, the data in each column must be formatted according to the element it will be stored in. Most elements (such as Text and Number) are straight-forward, but some require specific formatting:

Column formatting

Setting Rich Text values

This element will most likely require the most pre-processing; try to avoid complex HTML and text formatting. The list of supported HTML elements and their syntax can be found in the Management API reference. Notably, you can insert links to other content items or assets by referencing and ID or external ID using the format described in links in rich text.

For example: Buy our <a data-item-external-id="F4891FB5-5215-4795-8A6F-18A4F68394FD">new coffee</a>. You can also add inline content items using the supported syntax, for example <object type="application/kenticocloud" data-type="item" data-external-id="59713"></object>.

Or, you can use special macros designed for this add-on. In the list below, the identifier_type can be "id" or "external-id": macro description format example
link-item Inserts a link to a content item link-item:identifier_type:identifier:text ##link-item:id:5946ca5d-cebe-4be1-b5f0-4cd0a0e43fb5:coffee is good##
link-asset Inserts a link to an asset link-asset:identifier_type:identifier:text ##link-asset:id:0013263e-f2a9-40b1-9a3e-7ab6510bafe5:asset##
item Inserts an inline content item item:identifier_type:identifier ##item:external-id:article6##
component Inserts a component component:identifier_type:identifier ##component:id:ac21f31d-9d4f-4ec6-b666-90e0827ffde7

You can insert content item links, asset links, and inline content items by entering the HTML or macro manually. Or, you can use the Rich text macros menu found in the add-on to generate macros:

Macro menu

Components in rich text

You can use the rich_text_components column to store all components of a content item, regardless of how many Rich Text elements it contains. When exporting items from Kontent, the components are automatically generated. For example, if you have a Rich Text element that contains this component:

<object type="application/kenticocloud" data-type="component" data-id="382abced-bfb6-4ee9-a2d4-2c3b8cd8ba5d"></object>

then the rich_text_components must contain a component with that ID, or the import will fail. The format of components can be found in Management API reference. Using the above example, the rich_text_components value could look like this:

[{
  "id": "382abced-bfb6-4ee9-a2d4-2c3b8cd8ba5d",
  "type": {
    "codename": "article"
  },
  "elements": [
    {
      "element": {
        "id": "975bf280-fd91-488c-994c-2f04416e5ee3"
      },
      "value": "This is some text in the component"
    }
  ]
}]

You can use the Rich text macros menu to generate the JSON of a component if you don't wish to enter it manually. In this menu, you will select the content type of the component, and the elements of that type will appear. Once the values are populated, the macro and JSON are both generated:

Generated component

Importing the Content

Click the Kontent icon in the sidebar and open the Import menu. You have two options before starting the import:

Import options

After clicking the Run button, please wait while the script runs. When it’s finished, a new window Sheet is created containing a detailed record of the operations taken per-row and general information:

Import log

Locating and updating existing items

Depending on what information is available in the sheet, existing items in your project are located differently. This is the order in which values are used to locate existing items:

  1. __external_id__: If not found, a new item is created with this ID. For both new items and existing items found this way, you are able to update the name and codename of the content item if those values are present in the row.
  2. codename: If not found, a new item is created with this codename. When updating existing items found this way, you can only update the name.
  3. name: If not found, a new item is created with this name. For new items created this way, you can set the codename and external_id. If an existing item was found, you can only update the codename of the item.

Batching

The import process will run for a maximum of 30 seconds. If your Sheet contains a large amount of data that cannot be completed within 30 seconds, the import will be performed in batches. You will be shown a menu stating which rows were successfully processed, and you can click Resume to continue importing the Sheet.

Exporting items from Kontent

Using the Export menu, you can save your content items in Google Sheets, allowing you to create backups to import into the same project or even other projects. When you export your items, the add-on will generate a new Sheet for selected content type. Then, the Sheet will be populated with the content items of that type.

There is an optional Translate IDs setting you can enable before exporting. This add-on uses the Management API to retrieve your data, and references to objects like taxonomy, assets, etc. use the internal id of the object. If you enable this setting, the ID will be converted into either a codename or external_id if available.

This is helpful if you are considering exporting items from Project A and into Project B. Both projects may have a taxonomy term called "Events," but the id of the term will be different. If the codename is "events" in both projects, the content items which reference that taxonomy term can be imported without any issues.

Asset renditions

This add-on does't currently support exporting/importing asset renditions. Only the original asset reference will be exported to Google Sheets using this add-on.

Contributing

  1. Clone this repo and run npm i
  2. Create a Google App Script project
  3. Run clasp login to connect the repo to your Google account
  4. Make changes
  5. Run npm run deploy to push changes to your App Script project
  6. Test your changes with a Test Deployment in Google Sheets
  7. Create a pull request to merge your changes