gpt4 book ai didi

How can I import JSON to Google Sheets from Godot via Apps Script so my constants are in the first frozen column?(如何通过Apps脚本将JSON从GoDot导入到Google Sheet中,使我的常量位于第一个冻结的列中?)

转载 作者:bug小助手 更新时间:2023-10-25 14:12:15 27 4
gpt4 key购买 nike



I'm currently following this tutorial / resource: https://github.com/amzker/Gsheet_Godot
This outputs data sent from a Godot program to a sheet that looks like this,

我目前正在关注这篇教程/资源:https://github.com/amzker/Gsheet_Godot这会将从GoDot程序发送的数据输出到如下所示的工作表中,






































GreekAlphabet WhatHungryDogDo BananasGoodFor HowManyWords
Alpha The Bananas These
Beta Hungry Are Are
Delta Dog Nice Four
Gamma Eats Snacks Words


My goal is to upload data to a Google Sheet from Godot in a format that resembles this,

我的目标是将数据从戈多上传到谷歌工作表,格式与此类似,











































GreekAlphabet Alpha Beta Delta Gamma
WhatHungryDogDo The Hungry Dog Eats
BananasGoodFor Bananas Are Nice Snacks
HowManyWords These Are Four Words


The relevant apps script code is as follows:

相关APPS脚本代码如下:


function json(sheetName) {
const spreadsheet = SpreadsheetApp.openById("1h_KlXz9IWt2MtQQWYUSk4FJbIr02MbfXWU3ZRqY7U3I") //CHANGE WITH YOUR SHEET ID ( see url of you sheet d/)
const sheet = spreadsheet.getSheetByName(sheetName)
const data = sheet.getDataRange().getValues()
const jsonData = convertToJson(data)
return ContentService
.createTextOutput(JSON.stringify(jsonData))
.setMimeType(ContentService.MimeType.JSON)
}
function convertToJson(data) {
const headers = data[0]
const raw_data = data.slice(1,)
let json = []
raw_data.forEach(d => {
let object = {}
for (let i = 0; i < headers.length; i++) {
object[headers[i]] = d[i]
}
json.push(object)
});
return json
}
function doGet(params) {
const sheetname = params.parameter.sheetname
return json(sheetname)
}

function doPost(params) {
const datee = params.parameter.date
const timee = params.parameter.time
const catee = params.parameter.cate
const amounte = params.parameter.amount
const desce = params.parameter.desc
const sheetname = params.parameter.sheetname


if(typeof params !== 'undefined')
Logger.log(params.parameter);

var ss = SpreadsheetApp.openById("1h_KlXz9IWt2MtQQWYUSk4FJbIr02MbfXWU3ZRqY7U3I") //CHANGE WITH YOUR SHEET ID ( see url of you sheet d/)
var sheet = ss.getSheetByName(sheetname)
var Rowtoenter = sheet.getLastRow()+1
sheet.appendRow([datee,timee,catee,amounte,desce])


/*
var datecol = sheet.getRange(Rowtoenter,1)
var timecol = sheet.getRange(Rowtoenter,2)
var catecol = sheet.getRange(Rowtoenter,3)
var amountcol = sheet.getRange(Rowtoenter,4)
var descol = sheet.getRange(Rowtoenter,5)

datecol.setValue(datee)
timecol.setValue(timee)
catecol.setValue(catee)
amountcol.setValue(amounte)
descol.setValue(desce)
*/

}

When the data leaves Godot, it's in the form of a URL with a string of parameters appended.

当数据离开GoDot时,它的形式是一个附加了参数字符串的URL。



date=TODAY&time=NOW&cate=CATE&amount=AMOUNT&desc=DESC&flibble=SPONDS&sheetname=Sheet1



Once it reaches Sheets, it's converted into the first kind of table, where I really want it to appear as the second kind of table. =/

一旦它到达工作表,它就被转换成第一种表,在那里我真的希望它显示为第二种表。=/


I'm trying to wade through the source and read through the Docs (https://developers.google.com/apps-script/reference/spreadsheet/sheet) but I'm very new to working with JSON and I'm finding it hard to get a starting point.

我正在费力地阅读源代码和Docs(https://developers.google.com/apps-script/reference/spreadsheet/sheet),但我对JSON的工作非常陌生,我发现很难找到一个起点。


Any thoughts appreciated!

如有任何意见,敬请垂询!


(https://i.stack.imgur.com/EY0Yl.png)
(https://i.stack.imgur.com/kASx9.png)

(https://i.stack.imgur.com/EY0Yl.png)(https://i.stack.imgur.com/kASx9.png)


更多回答

Do NOT share spreadsheets/images as the only source of data, to avoid closure of the question. Make sure to add input and expected output as plain text table to the question. Click here to create a table easily, which are easier to copy/paste as well. Also, note that your email address can also be accessed by the public, if you share Google files.

不要将电子表格/图像作为唯一的数据来源,以避免问题结束。确保将输入和预期输出作为明文表添加到问题中。单击此处可轻松创建表格,也更易于复制/粘贴。此外,请注意,如果你共享谷歌文件,你的电子邮件地址也可以被公众访问。

I haven't only shared a spreadsheet/image as the only source of data - at the top of the question, there's a link to the Github page which includes the entire source code I'm working from. As far as I can tell, I'm not sharing Google files (the reason I went with images). I'll give your table link an attempt now...

我不仅分享了一个电子表格/图像作为唯一的数据来源--在问题的顶部,有一个指向Github页面的链接,其中包括我正在工作的整个源代码。据我所知,我没有分享谷歌文件(我使用图片的原因)。我现在要试一试你的桌子链接。

Please click the first link in my comment: spreadsheets. Github link is still external.

请点击我的评论中的第一个链接:电子表格。GitHub链接仍为外部链接。

Roger, will attempt to cooperate. I'm not sure where I'm to use the formula described in the third link of your original comment. Will post the relevant code from the Github link.

罗杰,我会尝试合作。我不知道该在哪里使用您最初评论的第三个链接中描述的公式。将从Github链接发布相关代码。

优秀答案推荐

const spreadsheet = SpreadsheetApp.openById("1h_KlXz9IWt2MtQQWYUSk4FJbIr02MbfXWU3ZRqY7U3I")

/** Requires sheet to already be in column-header format */
function convertToJson(data) {
return data.flatMap(([header, ...rowData]) => ({ [header]: [...rowData] }))
}

function json(sheetName) {

const data = spreadsheet.getSheetByName(sheetName).getDataRange().getValues()
const jsonData = convertToJson(data)

return ContentService
.createTextOutput(JSON.stringify(jsonData))
.setMimeType(ContentService.MimeType.JSON)

}

function doGet(params) {
return json(params.parameter.sheetname)
}

function doPost(params) {

const { date, time, cat, amount, desc, sheetname } = params.parameter

const sheet = spreadsheet.getSheetByName(sheetName)
const entryColumn = sheet.getLastColumn()+1

const columnData = [date, time, cat, amount, desc].map(item => [item])
sheet.getRange(1, entryColumn, columnData.length, 1).setValues(columnData)

}



If your sheet(s) are still in the "top row as header" format, you can run:

如果您的工作表(S)仍采用“顶行作为标题”的格式,则可以运行:


function convertSheet() {

const sheet = spreadsheet.getSheetByName(`**YOUR_SHEET**`)

const sheetValues = sheet.getDataRange().getValues()
const convertedValues = sheetValues[0].map((_, index) => sheetValues.flatMap(row => row[index]))

sheet.setFrozenRows(0)
sheet.setFrozenColumns(1)

sheet.getDataRange().clearContent()
sheet.getRange(1, 1, convertedValues.length, convertedValues[0].length).setValues(convertedValues)

}

更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com