marcusolsson / obsidian-projects

Plain text project planning in Obsidian
Apache License 2.0
1.38k stars 55 forks source link

How to "create new project" by code #917

Open zhangkaihua88 opened 2 months ago

zhangkaihua88 commented 2 months ago

What would you like to be added?

I would like to ask how to quickly create a project through code instead of a UI interface

like this app.plugins.plugins["obsidian-projects"].createProject()

Why is this needed?

Due to my many projects, I want to automate the creation

zhangkaihua88 commented 2 months ago

I implemented it by adding the following code in main.js

  addNewProject(data){
    data.id = v4_default()
    data.views.forEach(view => {
        view.id = v4_default();  // Setting the 'type' property to 2
      });
    settings.addProject(data);
    // this.activateView(data.id);
    return data.id
  }

Calling through JavaScript

a = {
  "fieldConfig": {},
  "defaultName": "",
  "templates": [],
  "excludedNotes": [],
  "isDefault": false,
  "dataSource": {
    "kind": "folder",
    "config": {
      "path": "14_Quant/因子学习",
      "recursive": true
    }
  },
  "newNotesFolder": "",
  "views": [
    {
      "config": {},
      "filter": {
        "conjunction": "and",
        "conditions": []
      },
      "colors": {
        "conditions": []
      },
      "sort": {
        "criteria": []
      },
      "name": "表格",
      "type": "table"
    }
  ],
  "name": "Quant_test"
};

app.plugins.getPlugin('obsidian-projects').addNewProject(a)