1

I'm trying to parse a YAML file within Google Apps Script. I would like to use the information from a YAML file in Google Sheets.

From what I understood, the underlying library is available (Use app engine yaml parser in scripts) but I don't seem to understand how I can use this library.

Thanks

1
  • Welcome to Stack Overflow. AFAIK libraries are intended to be used with specific programming languages. The question that you linked is about scripts written in Python but Google Apps Script uses JavaScript. Keep searching / learning. Commented Mar 2, 2022 at 22:51

1 Answer 1

3

I suppose it can depend on how complicated is your yaml. For simply cases you can use this lib https://code.google.com/archive/p/javascript-yaml-parser/downloads

Just download and unpack the latest .gz file and copy contents of the file yaml.js into your project:

enter image description here

Then you can convert your yaml string into a json object this way:

// your YAML sting

var string =
`foo: bar
stuff:
  foo: bar
  bar: foo
arr:
  - aaa
  - bbb
  - ccc
`

// JSON object

var obj = YAML.eval(string);

console.log(JSON.stringify(obj))

Output:

{
  "foo": "bar",
  "stuff": {
    "foo": "bar",
    "bar": "foo"
  },
  "arr": ["aaa", "bbb", "ccc"]
}
Sign up to request clarification or add additional context in comments.

2 Comments

The similar (but not quite) question is here: stackoverflow.com/questions/67898452/…
Thanks! This was the route that I went with and it worked out perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.