Send draft tags to multi-select property in notion

Hi,

I installed the great action “Notion: add page to Database + MD” to send drafts to a Notion database.

The script works great, but I would like to modify it to also send any drafts tags and store them in a multi-select property.

I was able to send information to a “multi-select” property called “Group” in my Notion database by adding/changing the “const properties” to:

const properties = {
“Note title”: {
“title”: [ {
“text”: { “content”: title}
}]
} ,
“Group”: {
“select”: {
“name”: tags
}
} ,
};

But I am unable to store the drafts tags in a variable called “tags”. I tried “let tags = draft.tags;” but without luck.

Can anyone point me in the right direction?

Thanks!

  • Kristian

Disclaimer: I don‘t use notion so it might not be that helpful :smiley:
The line you tried will definitely store the tags of the current draft in the tags variable:

let tags = draft.tags

They are stored as an array and depending on the tags you use this will look differently for you:

Are you saying that you successfully added information to the notion database with your changes or is this just an assumption?
Maybe the changed properties field is still wrong?
You could test this by using a static string (or what type does notion expect here?) instead of the „tags“ variable and see if notion accepts the input.
If it does then you might need to convert the array of tags to a readable format for notion.

A quick scan of the Notion API reference here: Property object also looks like you need to use a „multi-select“ type instead of the „title“ type of the existing action.
Maybe check the API reference further to see what this type of block needs as input. If you get stuck here maybe other Notion users here can jump in and help :slight_smile:

Thanks for your input!
Yes I successfully added information to the notion database with the changes mentioned above (by sending the title to both the title property and the property which I would like to receive the tags.

The “title” which I am successfully able to send to notion is stored by the command:

const title = lines.shift().replace(/^# /, “”);

I also tried:

const tags = draft.tags;

and

const tags = draft.tags;

But non of them seems to work.
Most of the variables are stored in “const” in this script. Would I maybe have to adapt and use something else than “let”. Or can “draft.tags” not be used with “const”?

You’re using a „select“ in the properties and write about a „multi-select“ in the text.

As mentioned I don’t use notion but looking at the API description it seems to me that your input for the select field is not working right now. Are you sure that you also added information to the select/multi-select field with the mentioned properties? If yes can you show that?

Also look at the documentation which says that „,“ are not allowed in the field which might be also a reason for the failure:

Hi FlohGro,

You have sharp eyes and sorry for the confusion. I have tested both select and multi-select and have been able to send information to both properties.

If I define the variable “tags” like this:

const tags = “test2”;

I am able to send it to the notion properties “select” and “rich text” (tried this due to what you said about comma) by adding/changing the following lines in the script:

const properties = {
“Note title”: {
“title”: [ {
“text”: { “content”: title}
}]
} ,
“Group”: {
“select”: {
“name”: tags
}
} ,
“Text2”: {
“rich_text”: [ {
“text”: { “content”: tags}
}]
} ,
};

However if I try to include the tags of the current draft open with this code:

const tags = draft.tags;

Nothing arrives in notion. I tried sending the tags to both the “select” and “rich_text” individually without success. Thanks for your attempts!! I’ve started reading the Eloquent Javascript, maybe ut will provide some answers :stuck_out_tongue:

You need to check how you can send multiple elements to the blocks in notion. draft.tags will be an array of strings. So if your draft has just one tag it will still be an array with one string element. Maybe notion doesn’t accept that type as property.

Next step would be to try adding two tags with the test tag you figured out by yourself. And then you need to convert the array of strings to a type / string with separators between the elements that are accepted by notion.

Hope that input helped you again :slight_smile: good luck :+1:t3:

Hi,

I finally figured it out how to send tags (even though I don’t really understand why it worked). The answer was found in the “Send to Obsidian with Tags” action.

The code which now works to store tags in a variable I am able to send to notion is as follows:

let tags = draft.tags;
let ctags = “”;
for (let i = 0; i < tags.length;i++) {
let nstag = tags[ i].replaceAll( ’ ', ‘_’) + i;
ctags += nstag;
ctags += " ";
}

I then use “ctags” to recall the stored tags.
This works if I send the tags to a “rich_text” or “select” property, but all tags will be sent as a single tag with space between.
However to send tags as multiple entries they must be separated as follows:

{ “name”: “tag1” }, { “name”: “tag2” }, { “name”: “tag3” }

I could accomplish this by creating the line above in a loop but I am unable to include " in a variable.

How could I include the line above in a variable such as this:

let tags = { “name”: “tag1” }, { “name”: “tag2” }, { “name”: “tag3” }

Thank you again!

I’m a little confused…that first snippet you included loops over the array of tag names and creates a string variable replacing any spaces in the tag names with underscores, separating each tag with a space. So if the draft had “tag1” and “tag2” assigned, the draft.tags would be ["tag1", "tag2"] and that could would turn it into the string "tag1 tag2".

Below you are saying Notion wants each tag as an object, with a name property, like { "name": "tag1" }. If you have multiple, in Javascript you would still need to put them in an array with square brackets to make that valid, like:

let tags = [{ “name”: “tag1” }, { “name”: “tag2” }, { “name”: “tag3” }]

(You are confusing yourself with the let const - those don’t effect the content of the variable, just keywords that change whether the variable can be reassigned later in the script - const == constant that is not going to change)

Hi,

Yes, was a bit arbitrary what I included in the variable and in the code further down on the page. What is now working is:

let tags = draft.tags;
let ctags = “”;
for (let i = 0; i < tags.length;i++) {
let nstag = tags[ i].replaceAll( ’ ', ‘_’);
ctags += nstag;
}
const tags2 = “test2”;
const utags = [ { “name”: ctags }, { “name”: tags2 }, ];

And further down in the script i recall it with:

const properties = {
“Note title”: {
“title”: [ {
“text”: { “content”: title}
}]
} ,
“tags”: {
“multi_select”:
utags
} ,
};

This code looks a bit stupid but was just to test if I could actually include “[ { “name”: ctags }, { “name”: tags2 }, ];” in a variable.

What I now am trying to do is to use the “for (let i = 0; i < tags.length;i++)” loop in the script to build up the variable utags to “[ { “name”: tag1 }, { “name”: tag2 }, { “name”: tag3 }, ]” (etc).

I have problems with including quotes in a variable. Which might be some of the reason I am unable to make this loop. I tried to include the following in the loop without success (last and third line outside the loop):

let temptag = "{ "name": " + nstag + "}, "
otags += temptag
const utags = [ otags ];

So I’m still a bit stuck with the script. I am able to export drafts tags to a notion database and store as “select” property. This is however limited to a single tag. I would like to be able to export any number of tags and store them in a “multi_select” property. To store multiple tags in a notion multi_select property I changed the properties in the “Notion: add page to Database + MD ” action to the following:

let notetags = [
{ “name”: “tag1” },
{ “name”: “tag2” },
{ “name”: “tag3” },
] ;
const properties = {
“Note title”: {
“title”: [ {
“text”: { “content”: title}
}]
} ,
“Tags”: {
“multi_select”:
notetags
} ,
};

To store tags as a string in a variable I currently use:

let tags = draft.tags;
let ctags = “”;
for (let i = 0; i < tags.length;i++) {
let nstag = tags[ i].replaceAll( ’ ', ‘_’);
ctags += nstag;
}

Anyone knows how I can make a loop create the following for any given number of tags and storing it in the variable “notetags”?
(tag 1, tag2, tag3, etc being the tags of the current draft).

  						[ { "name": "tag1" },
  						{ "name": "tag2" },
  						{ "name": "tag3" },
							] ;

Several ways, easiest is to map the values of the tag array into a different array, like:

let tags = draft.tags.map(t => {
	return { "name" : t }
})

Thank you for the tip! I will play around with that technique and see if I figure it out!

Feels like I’m very close but still no sigar.

To repeat I try to save the tags of a document (the tags are “tag1”, “tag2”, and “tag3”) into the format under to export it to a Notion multi_select property. In this example I manually inserted “tag1”, “tag2”, “tag3”, but the goal is to make the script insert an undefined number of tags into the same format with a loop (or other methods if they exist).

  let notetags = [
  					{ "name": "tag1" },
  					{ "name": "tag2" },
  					{ "name": "tag3" },
					] ;

Later in the script this is recalled as follows:

  "Tags": {
		"multi_select": 	
  			notetags
  		} ,

So far so good. Then I tried to use the drafts.tags.map function agiletortoise recommended. I ended up with the following script:

  let temptags = draft.tags.map(t => {
  		return { "name" : t }
		})
  let notetag = "[ ";
  for (let i = 0; i < temptags.length;i++) {
  		notetag += temptags[i];
  		}
  notetag += " ]"

I first tried to imput this into a “rich_text” property in the Notion database which is more forgiving and might give tips about what is wrong. I was able to create an entry but the the result was

[ [object Object][object Object][object Object] ]

I then tried to convert the object/array to a string with stringify and added a comma between elements:

  let temptags = draft.tags.map(t => {
  		return { "name" : t }
		})
  let notetag = "[ ";
  for (let i = 0; i < temptags.length;i++) {
  		notetag += JSON.stringify(otags[i]) + ", ";
  		}
  notetag += " ]"

The output is now:

[ {“name”:“tag1”}, {“name”:“tag2”}, {“name”:“tag3”}, ]

Which is exactly what I want, but when I tried to recall this string to the multi_select property no entry in Notion is created (but no error in drafts).

Any idea why it is still not working? I have a feeling it has something to do with properties of objects/arrays vs string but not able to figure it out…

You would definitely save yourself some frustration by reading up on basic datatypes in Javascript (the Eloquent Javascript book is amazing for these basic and free online). You are getting a little turned around turning things into strings that should not be strings.

The tags variable after that map function is already what you need! It does not need any further treatment to send to Notion. It is an array type data structure, containing three Javascript objects…each with single key: value pair - where name = tag.

Pretty much all the values the Notion API expects are either Javascript objects, or (as in this case) arrays of Javascript object.

Hi again,

Yes, that’s why this is so frustrating :crazy_face:

In theory I also though your code should then be enough to save the tags in an array:

let tags = draft.tags.map(t => {
    return { "name" : t }
  	})

But if I try to send it to a notion multi_select property with the code below nothing happends.

const properties = {
	“Note title”: {
		“title”: [ {
			“text”: { “content”: title}
			}]
		} , 
	“Tags”: {
		“multi_select”: 
			tags
		} , 
	};

Thank you for the tip. I just started reading the Eloquent Javascript in addition to the w3schools website which was quite useful (JavaScript Arrays).

Any tips on how I can change the lines in the “const properties” to store note tags in the “tags” array to the multi_select property?

“Nothing happens” = ??? (no tags assigned?, error?)

Were you testing with a draft that had tags assigned? The map function as I provided it is mapping the tag values assigned to the current draft in the editor.

Sorry for not being clear. No entry/page is created in the notion database, meaning that non of the text in the draft is put in the database either. Yes, I have 3 tags in the currently open draft called “tag1”, “tag2”, and “tag3”.

If I leave everything identical and replace:

let tags = draft.tags.map(t => {
return { “name” : t }
})

with:

let tags = [ {“name”:“tag1”}, {“name”:“tag2”}, {“name”:“tag3”}, ];

The entry is correctly created in my notion database with my drafts note as the page with drafts note title as page title and the tags “tag1”, “tag2”, and “tag3” being assigned in the multi_select property “Tags”.

It seems that the formatting of the array created with:

let tags = draft.tags.map(t => { return { “name” : t } })

somehow has the wrong formatting or something.

Those should output the same thing if the current draft loaded in the editor has the tags “tag1”, “tag2”, and “tag3” assigned. Does it?

If you add a alert(JSON.stringify(tags)) after the let tags = line, what does it display?

Are you getting an error in the action log when you run this action?

soooooo… this is maybe driving me even more crazy!!! I went back to reproduce the problem and use your “alert(JSON.stringify(tags)” and get the action log.

BUT…

IT SUDDENLY WORKED.

It makes no sense. This is exactly the same code I tried a hundred times. Yes, I understand it obviously could not have been… Anyway… Thank you very much agiletortoise for all your help!!

Now I have no excuse but to actually try to get some real work done :stuck_out_tongue:

Have a great day!

2 Likes

I made the original script which was a bit mashed together - thanks for working this thru - it was very helpful discussion :smile:

1 Like