美文网首页
Convert a Google Spreadsheet to

Convert a Google Spreadsheet to

作者: 大漠孤烟直_v | 来源:发表于2022-08-17 16:10 被阅读0次

一、下载安装Node.js 并配置好环境变量

1.https://nodejs.org/zh-cn/download/
image.png

二、下载localize-with-spreadsheet-2

https://github.com/NeverwinterMoon/localize-with-spreadsheet-2

1.下载后 在当前文件夹下执行 npm install localize-with-spreadsheet-2
成功后会多出这三个文件:

1660721307543.png

2.在当前文件夹底下创建 update-localization.js 文件
内容:

const Localize = require('localize-with-spreadsheet-2')
Localize.fromGoogleSpreadsheet('[api-key]', '[spreadsheet-key]', '*')
  .then(localizer => {
    localizer.setKeyCol('KEY') // name of the column containing the translation key

    Array.from(['en', 'de']).forEach(language => localizer.save(
      `project-name/resource/${language}.lproj/Localizable.strings`,
    //`project-name/resource/values-${language}/strings.xml`,
      { valueCol: language, format: 'ios' } // format can also be 'android' or 'json'
    ))
  })

二、申请api key 及权限

文档:https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication?id=api-key
1.首先需要启用Google Sheet api ,成功后会在控制台看到

1660722135207.png

2.创建api


1660722244902.png

成功后


1660722325112.png

这个API密钥就是js文件 中的[api-key]

三、创建一个google 在线文档

例如:https://docs.google.com/spreadsheets/d/1IUQnoVbkUYMl36mAm3ts27pQxg0D1WKASM9wy0s_tNI/edit#gid=0
1IUQnoVbkUYMl36mAm3ts27pQxg0D1WKASM9wy0s_tNI 就是js文件 中的[spreadsheet-key]
1.将此文档发布

image.png
2.设置权限 设置成所有人拿到链接都可访问
1660722786886.png

四、执行js文件

node update-localization.js
成功后会生成你想要的的东西

1660723175561.png

五、趟坑

image.png

报错400 是因为api key 填写错误,或者是你的账号并没有开启Google Sheet Api 功能

image.png

报错403 是因为第三步的第2步 没有给到权限

image.png

报错 value.replace is not a function 解决方案


image.png

我的最终的js 文件仅供参考

const Localize = require('localize-with-spreadsheet-2')

Localize.fromGoogleSpreadsheet('AIzaS************pLrDwz9udWA', '1f6Dke6*********JTvyjSSxja2_wlP2MawVWsnHEak', '*')
  .then(localizer => {
    localizer.setKeyCol('key') // name of the column containing the translation key

    Array.from(['zh-chs','en', 'pt','es']).forEach(language => localizer.save(
      `project-name/resource/values-${language}/strings.xml`,
      { valueCol: language, format: 'android' } // format can also be 'android' or 'json'
    ))
  })

相关文章

网友评论

      本文标题:Convert a Google Spreadsheet to

      本文链接:https://www.haomeiwen.com/subject/ofgjgrtx.html