Can only office add custom macro code? How to interact with macro code through code?

My requirement is to retrieve the current page number while reading the document in the editor, so that it can automatically redirect to the last page viewed during the next loading. Since I couldn’t find the relevant API, I want to implement this by calling macro code. My macro code is as follows:

const fullMacro = 
    (function () {
    debugger;
      function getPage(){
        console.log('宏代码获取当前页码执行');
        let doc = Api.GetDocument();
        let page = doc.GetCurrentVisiblePages();
        console.log("当前可见页面:", page);
        return {page: page};
      }

      function gotoPage(){
        let doc = Api.GetDocument();
        console.log('宏代码跳转页码执行');
        if (toPage){
          doc.GoToPage(50);
        }
      }

      Api.AddCustomFunction(getPage);
      Api.AddCustomFunction(gotoPage);
      getPage();
      gotoPage()
    })();
  ;

Hello @free

As I can see, the page index in your sample is hardcoded to 50. Is your goal to switch to this exact page? If so and your macro works, then you can just put it to autostart.

However, if your goal is to dynamically switch pages between editing sessions, then you have to keep in mind that macros run in isolated environment and they are not accessible from outside the macro editor.

UPD: According to other replies in various topics, you would like to specify the page from outside the editor frame. Automation API is the only tool that allows interacting with the editor from outside the editor frame. Please find more information about it here: Automation API | ONLYOFFICE