Processing content and split string with new line \n as separator

Incoming triggers from discord for example returns a content such as:

Email:
user@gmail.com

Name:
John Doe

How do I split the content so the email and name can be sent as new row in google sheet?

Hi @bjornberinger

First off, welcome to the Pipedream community. Happy to have you!

I assume that this content is passed as a value in one field called content for example.

You could potentially just split the content on the newline character (\n) in the string with a Node.js step:

export default defineComponent({
  props: {
    content: {
       type: 'string',
       label: 'Discourse content',
    }
  },
  async run({ steps, $ }) {
    // Split the content on newline characters
    const parts = this.content.split('\n')
    $.export('parts', parts);
    
    // if the email is aways the 2nd line, access it this way:
    $.export('email', parts[1]);

    // if the name is always the 5th line, access it this way:
    $.export('name', parts[4]);
  }
})
1 Like

Thank you! I have zero experience with node.js so I’m kind of in trouble here. :pray:

export default defineComponent({
  props: {
    content: {
       type: 'string',
       label: 'Discourse content',
  },
  async run({ steps, $ }) {
    // Split the content on newline characters
    const parts = steps.trigger.content.split('\n')
    $.export('parts', parts);
    
    $.export('walletAddress', parts[1]);

  },
}})

I have that code above and when I went to test it I encountered an error that says

Code Error
type missing for prop run

If it helps, my steps go as Discord > Node.js > Google Sheet and yup I wanted to split a ‘content’ that the bot pulls off discord.

Sorry about that! I had a few small mistakes.

Try this version instead:

export default defineComponent({
  props: {
    content: {
       type: 'string',
       label: 'Discourse content',
    }
  },
  async run({ steps, $ }) {
    // Split the content on newline characters
    const parts = this.content.split('\n')
    $.export('parts', parts);
    
    // if the email is aways the 2nd line, access it this way:
    $.export('email', parts[1]);

    // if the name is always the 5th line, access it this way:
    $.export('name', parts[4]);
  }
})

1 Like

Yes that works!

Thank you so much, Pierce! :pray:

1 Like