[英]How do I get Figma API to work with the Google App-script API?
I am thinking of creating a google slides to Figma exporter using Google App Script.我正在考虑使用 Google App Script 为 Figma 导出器创建一个 google 幻灯片。 Starting out I would first like to route the shapes created in from google Slides to figma.
一开始,我首先想将在谷歌幻灯片中创建的形状路由到 figma。 How would I go about setting up my file?
我将如何设置我的文件? And I don't know how to set up the Oauth api communication between Google and Figma or if it's even possible.
而且我不知道如何设置 Google 和 Figma 之间的 Oauth api 通信,或者是否有可能。
I believe that I can start with:我相信我可以从:
https://github.com/figma/plugin-samples/blob/master/react/src/code.ts https://github.com/figma/plugin-samples/blob/master/react/src/code.ts
https://github.com/gsuitedevs/apps-script-samples/blob/master/slides/style/style.gs#L30 https://github.com/gsuitedevs/apps-script-samples/blob/master/slides/style/style.gs#L30
var file=projectid.key()
var=figma rectangle= file()
await figma.loadFontAsync({ family: "Roboto", style: "Regular" })
name;
var figmaShape = {
figma.ui.onmessage = msg => {
if (msg.type === 'create-rectangles') {
const nodes = []
for (let i = 0; i < msg.count; i++) {
const rect = figma.createRectangle()
rect.x = i * 150
rect.fills = [{type: 'SOLID', color: {r: 1, g: 0.5, b: 0}}]
figma.currentPage.appendChild(rect)
nodes.push(rect)
}
figma.currentPage.selection = nodes
figma.viewport.scrollAndZoomIntoView(nodes)
}
figma.closePlugin()
}
};
var powerpointfile = driveApp.getFileById = ("### Slide file ID ###")
function powerPointShape = () {
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300,
getObjectId.element(SHAPE);
};
file.getSlides.shape = (powerPointShape, ) => {
this.powerPointShape.getRigh()=this.figmaShape(rect.x);
this.powerPointShape.getleft()=this.figmaShape(rect.y);
}
But from there would I also want to get the file id from google app script to a Figma File?但是从那里我还想从谷歌应用脚本获取文件 ID 到 Figma 文件吗?
and after looking at: https://github.com/alyssaxuu/figma-to-google-slides/blob/master/Chrome%20Extension/background.js I wonder if I would have to create a chrome extension or a google Slides plugin.看了之后: https://github.com/alyssaxuu/figma-to-google-slides/blob/master/Chrome%20Extension/background.js我想知道我是否需要创建一个 chrome 扩展程序或一个谷歌幻灯片插件.
How about this answer?这个答案怎么样?
Unfortunately, it seems that the shapes of Google Slides cannot be put to the page of Figma file.不幸的是,谷歌幻灯片的形状似乎无法放到 Figma 文件的页面上。 Because it seems that there are no methods of API for putting the shapes.
因为似乎没有 API 的方法来放置形状。 But it was found that that the pages of Figma file can be retrieved as the image using Figma API.
但是发现使用 Figma API 可以将 Figma 文件的页面检索为图像。
In this answer, I would like to propose the sample script that the pages of Figma file can be put to the Google Slides as the image using Figma API with the access token.在这个答案中,我想提出一个示例脚本,可以使用带有访问令牌的 Figma API 将 Figma 文件的页面作为图像放入 Google 幻灯片。 So you can directly use Figma API with Google Apps Script.
所以你可以直接使用 Figma API 和 Google Apps Script。
You can see the method for retrieving the access token at here .您可以在此处查看获取访问令牌的方法。 Although there is also OAuth2 for retrieving the access token, in your situation, I thought that the method for directly generating the access token on the site might be suitable.
虽然也有用于检索访问令牌的 OAuth2,但在您的情况下,我认为在站点上直接生成访问令牌的方法可能是合适的。 So in this answer, the generated access token on the site is used.
所以在这个答案中,使用了网站上生成的访问令牌。 Please retrieve the access token as follows.
请按如下方式检索访问令牌。
Generate a personal access token
生成个人访问令牌
- Login to your Figma account.
登录到您的 Figma 帐户。
- Head to the Account Settings from the top-left menu inside Figma.
从 Figma 的左上角菜单转到帐户设置。
- Find the Personal Access Tokens section.
找到个人访问令牌部分。
- Click Create new token.
单击创建新令牌。
- A token will be generated.
将生成一个令牌。 This will be your only chance to copy the token, so make sure you keep a copy of this in a secure place.
这将是您复制令牌的唯一机会,因此请确保将其副本保存在安全的地方。
The access token is like #####-########-####-####-####-############
.访问令牌类似于
#####-########-####-####-####-############
。 At Google Apps Script, the authorization is done by headers: {"X-Figma-Token": accessToken}
.在 Google Apps Script 中,授权由
headers: {"X-Figma-Token": accessToken}
。
In order to retrieve the Figma file using Figma API, the file key is required.为了使用 Figma API 检索 Figma 文件,需要文件密钥。 You can retrieve the file key from the URL of the file.
您可以从文件的 URL 中检索文件密钥。
The URL of the file is like https://www.figma.com/file/###/sampleFilename
.文件的 URL 类似于
https://www.figma.com/file/###/sampleFilename
。 In this case, ###
is the file key.在这种情况下,
###
是文件密钥。
The sample script is as follows.示例脚本如下。 Before you run the script, please set the variables of
accessToken
and fileKey
.在运行脚本之前,请设置
accessToken
和fileKey
的变量。
function myFunction() {
var accessToken = "###"; // Please set your access token.
var fileKey = "###"; // Please set the file key.
var baseUrl = "https://api.figma.com/v1";
var params = {
method: "get",
headers: {"X-Figma-Token": accessToken},
muteHttpExceptions: true,
};
var fileInfo = JSON.parse(UrlFetchApp.fetch(baseUrl + "/files/" + fileKey, params));
var children = JSON.parse(UrlFetchApp.fetch(baseUrl + "/images/" + fileKey + "?format=jpg&scale=3&ids=" + fileInfo.document.children.map(function(c) {return c.id}).join(","), params));
if (!children.err) {
var s = SlidesApp.create("sampleSlide");
var slide = s.getSlides()[0];
var keys = Object.keys(children.images);
keys.forEach(function(c, i) {
slide.insertImage(children.images[c]);
if (i != keys.length - 1) slide = s.insertSlide(i + 1);
})
} else {
throw new Error(children);
}
}
myFunction()
is run, at first, the file information is retrieved with the file key fileKey
.myFunction()
时,首先使用文件密钥fileKey
检索文件信息。 Then, all pages are retrieved from the retrieved file information, and the retrieved pages are put to each slide of new Google Slides. If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.