简体   繁体   English

如何让 Figma API 与 Google App-script API 一起使用?

[英]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:我相信我可以从:

References参考

Figma reference Figma 参考

https://github.com/figma/plugin-samples/blob/master/react/src/code.ts https://github.com/figma/plugin-samples/blob/master/react/src/code.ts

google app script reference谷歌应用脚本参考

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

Get Figma Shape获取 Figma 形状

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()
}
};

Get Google Docs File Shape获取 Google Docs 文件形状

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);
                              };

Create new Figma file#创建新的 Figma 文件#

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?这个答案怎么样?

Issue and workaround:问题和解决方法:

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。

Usage:用法:

1. Retrieve access token 1.检索访问令牌

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生成个人访问令牌

  1. Login to your Figma account.登录到您的 Figma 帐户。
  2. Head to the Account Settings from the top-left menu inside Figma.从 Figma 的左上角菜单转到帐户设置。
  3. Find the Personal Access Tokens section.找到个人访问令牌部分。
  4. Click Create new token.单击创建新令牌。
  5. 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}

2. Retrieve file key 2.检索文件密钥

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.在这种情况下, ###是文件密钥。

3. Run script 3.运行脚本

The sample script is as follows.示例脚本如下。 Before you run the script, please set the variables of accessToken and fileKey .在运行脚本之前,请设置accessTokenfileKey的变量。

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);
  }
}
  • When 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.然后,从检索到的文件信息中检索所有页面,并将检索到的页面放到新的Google Slides的每张幻灯片中。
  • I think that the action of this script is similar to the script which is shown at the bottom of your question.我认为此脚本的操作类似于您问题底部显示的脚本

Note:笔记:

  • This is a sample script.这是一个示例脚本。 So please modify it for your actual situation.所以请根据您的实际情况进行修改。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 Snipe-IT API 使用 Google 应用脚本检索数据并将其填充到 Google 表格中? - How do I get Snipe-IT API to retrieve data using a Google App Script and populate it on a Google Sheet? 如何使Steam API正常工作? - How do I get the steam API to work? 如何使文本对齐:在谷歌应用脚​​本(日历)中居中? - How to make text-align: center in google app-script (Calendar)? 由于长请求,谷歌应用脚本奇怪的 CORS 错误 - google app-script weird CORS error because of long request 如何使用Google Apps脚本通过USPS API获取国内运费? - How do I get domestic shipping rates via the USPS API using Google Apps Script? 我可以使用Google Geocode API,但不能使用Google Place API - I can get Google Geocode API to work, but not the Google Place API DailyMotion API 与 Google AppScript 的简单 GET 方法返回 503 错误。 如何让它始终如一地工作? - Simple GET method for DailyMotion API with Google AppScript is returning 503 error. How do I get it to work consistently? 从网站 HTML 表中获取数据并使用 App-Script 传输到 Google 表格 - Grab data from website HTML table and transfer to Google Sheets using App-Script 我无法在 google 应用程序脚本下输入工作 - I cannot get input to work under google app script 如何使用Google App脚本中的JavaScript从Google电子表格数据库中获取日期值 - How do i get the date value from google spreadsheet database using javascript in google app script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM