Class GoogleSheetsExtension
Provides an editor interface to GoogleSheets.
Inherited Members
Namespace: UnityEditor.Localization.Plugins.Google
Assembly: Unity.Localization.Editor.dll
Syntax
[Serializable]
public class GoogleSheetsExtension : CollectionExtension
Examples
This example adds and configures a GoogleSheetsExtension to a StringTableCollection.
public static class SetupExtensionSample
{
[MenuItem("CONTEXT/StringTableCollection/Add Configured Google Sheet Extension")]
public static void AddAndConfigureExtension(MenuCommand command)
{
var collection = command.context as StringTableCollection;
// Get or add a new GoogleSheetsExtension.
var googleExtension = collection.Extensions.FirstOrDefault(e => e is GoogleSheetsExtension) as GoogleSheetsExtension;
if (googleExtension == null)
{
googleExtension = new GoogleSheetsExtension();
collection.AddExtension(googleExtension);
}
// Clear old data.
googleExtension.Columns.Clear();
// We need configure what each column will contain in the sheet
var columnMappings = new List<SheetColumn>
{
// Column A will contain the Key
new KeyColumn { Column = "A" },
// Column B will contain any shared comments. These are Comment Metadata in the Shared category.
new KeyCommentColumn { Column = "B" },
// Column C will contain the English Locale and any comments that are just for this Locale.
new LocaleColumn { Column = "C", LocaleIdentifier = "en", IncludeComments = true },
};
// Assign the columns to the extension
googleExtension.Columns.AddRange(columnMappings);
// Assign our Google Sheets service asset
const string pathToYourAsset = "Assets/Google Sheets Service.asset"; //The path to your SheetsServiceProvider asset. See docs for further info.
var sheetsServiceProvider = AssetDatabase.LoadAssetAtPath<SheetsServiceProvider>(pathToYourAsset);
googleExtension.SheetsServiceProvider = sheetsServiceProvider;
googleExtension.SpreadsheetId = "My spread sheet id"; // We need to provide the Spreadsheet id. This can be found in the url. See docs for further info.
googleExtension.SheetId = 123456; // This is the id of the sheet in the Google Spreadsheet. it will be in the url after `gid=`.
// Mark the collection dirty so that the changes are saved
EditorUtility.SetDirty(collection);
}
}
This example uses the data that was configured in the GoogleSheetsExtension to perform a Push.
[MenuItem("Localization/Google Sheets/Push With Google Extension")]
public static void PushWithExtension()
{
// You should provide your String Table Collection name here
var tableCollection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
var googleExtension = tableCollection.Extensions.FirstOrDefault(e => e is GoogleSheetsExtension) as GoogleSheetsExtension;
if (googleExtension == null)
{
Debug.LogError($"String Table Collection {tableCollection.TableCollectionName} Does not contain a Google Sheets Extension.");
return;
}
PushExtension(googleExtension);
}
static void PushExtension(GoogleSheetsExtension googleExtension)
{
// Setup the connection to Google
var googleSheets = new GoogleSheets(googleExtension.SheetsServiceProvider);
googleSheets.SpreadSheetId = googleExtension.SpreadsheetId;
// Now send the update. We can pass in an optional ProgressBarReporter so that we can updates in the Editor.
googleSheets.PushStringTableCollection(googleExtension.SheetId, googleExtension.TargetCollection as StringTableCollection, googleExtension.Columns, new ProgressBarReporter());
}
This example shows how to use the data that was configured in a Google Sheets extension to perform a pull.
[MenuItem("Localization/Google Sheets/Pull With Google Extension")]
public static void PullWithExtension()
{
// You should provide your String Table Collection name here
var tableCollection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
var googleExtension = tableCollection.Extensions.FirstOrDefault(e => e is GoogleSheetsExtension) as GoogleSheetsExtension;
if (googleExtension == null)
{
Debug.LogError($"String Table Collection {tableCollection.TableCollectionName} Does not contain a Google Sheets Extension.");
return;
}
PullExtension(googleExtension);
}
static void PullExtension(GoogleSheetsExtension googleExtension)
{
// Setup the connection to Google
var googleSheets = new GoogleSheets(googleExtension.SheetsServiceProvider);
googleSheets.SpreadSheetId = googleExtension.SpreadsheetId;
// Now update the collection. We can pass in an optional ProgressBarReporter so that we can updates in the Editor.
googleSheets.PullIntoStringTableCollection(googleExtension.SheetId, googleExtension.TargetCollection as StringTableCollection, googleExtension.Columns, reporter: new ProgressBarReporter());
}
This example shows how to push every StringTableCollection that contains a GoogleSheetsExtension.
[MenuItem("Localization/Google Sheets/Push All Google Sheets Extensions")]
public static void PushAllExtensions()
{
// Get every String Table Collection
var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
foreach (var collection in stringTableCollections)
{
// Its possible a String Table Collection may have more than one GoogleSheetsExtension.
// For example if each Locale we pushed/pulled from a different sheet.
foreach (var extension in collection.Extensions)
{
if (extension is GoogleSheetsExtension googleExtension)
{
PushExtension(googleExtension);
}
}
}
}
This example shows how to pull every StringTableCollection that contains a GoogleSheetsExtension.
[MenuItem("Localization/Google Sheets/Pull All Google Sheets Extensions")]
public static void PullAllExtensions()
{
// Get every String Table Collection
var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
foreach (var collection in stringTableCollections)
{
// Its possible a String Table Collection may have more than one GoogleSheetsExtension.
// For example if each Locale we pushed/pulled from a different sheet.
foreach (var extension in collection.Extensions)
{
if (extension is GoogleSheetsExtension googleExtension)
{
PullExtension(googleExtension);
}
}
}
}
Properties
Name | Description |
---|---|
Columns | The column mappings. Each SheetColumn represents a column in a Google sheet. The column mappings are responsible for converting to and from cell data. |
RemoveMissingPulledKeys | If this value is set then after PullIntoStringTableCollection(int, StringTableCollection, IList<SheetColumn>, bool, ITaskReporter, bool) any keys that were not in the sheet will be removed. This is useful if you want to use a single sheet and will be adding and removing keys however if using multiple sheets then this value should be false to prevent accidental loss of data. |
SheetId | The id of a sheet inside of a Google Spreadsheet. Each tab is a separate sheet. The sheet id can be found in the url after the gid section: https://docs.google.com/spreadsheets/d/>SpreadsheetId/edit#gid=SheetId |
SheetsServiceProvider | The SheetsServiceProvider provides the authorization and connection to the Google Sheets service. |
SpreadsheetId | The Id of the Google Sheet. This can be found by examining the url: https://docs.google.com/spreadsheets/d/>SpreadsheetId/edit#gid=SheetId Further information can be found here. |