2017-11-15 1 views
0

커서 위치에 현재 날짜를 삽입하는 "유틸리티"-> "날짜 삽입"이라는 메뉴 항목을 추가하는 Google 문서 도구 용 스크립트를 만들었습니다. 이 스크립트를 다른 문서에 재사용하고 결국 조직의 다른 사람들에게 제공하기를 원하므로 다른 문서에 라이브러리로 가져 왔습니다. 그러나 그것을 실행하려고하면 라이브러리 내의 한 함수가 다른 함수를 참조하므로 .insertAtCursor 함수가 존재하지 않는다는 오류가 발생합니다.라이브러리 내 기능 참조

동일한 기능 내에서 라이브러리의 모든 것을 만들 필요가 있습니까? 그렇지 않은 경우 전화를 사용하여 라이브러리 내에 있다고 가정합니다. 나는 이것이 Google Apps Script의 범위에 대한 나의 오해라고 생각하지만 확실하지 않습니다. 여기

은 라이브러리입니다 :

function onOpen() { 
    // Add a menu with some items, some separators, and a sub-menu. 
    DocumentApp.getUi().createMenu('Utilities') 
     .addItem('Insert Date (dd MMMM yyyy)', 'insertAtCursor') 
     .addToUi(); 
} 




/** 
* Inserts the sentence "Hey there!" at the current cursor location in boldface. 
*/ 
function insertAtCursor() { 
    var cursor = DocumentApp.getActiveDocument().getCursor(); 

    if (cursor) { 
    // Attempt to insert text at the cursor position. If insertion returns null, 
    // then the cursor's containing element doesn't allow text insertions. 
    // Date format defined at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 
    // Make sure if you change the date format you update the menu items in the onOpen() fuction above 
    var date = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy"); // "yyyy-MM-dd'T'HH:mm:ss'Z'" 
    var element = cursor.insertText(date); 
    if (element) { 
     element.setBold(false); 
    } else { 
     DocumentApp.getUi().alert('Cannot insert text at this cursor location.'); 
    } 
    } else { 
    DocumentApp.getUi().alert('Cannot find a cursor in the document.'); 
    } 
} 

그리고 여기에 내가 다른 문서에서 사용하는 방법입니다

function onOpen() { 
    InsertDate.onOpen() 
} 

나는 작동하는 다른 문서에서 InsertDate.insertAtCursor 부르지 만, 아무튼 경우 메뉴 항목에서 해당 함수를 호출하기 때문에 의미가 없습니다. 이 라이브러리를 다른 문서에서 "그냥 작동"하도록 수정하는 방법을 잘 모르겠습니다.

function onOpen() { 

    // Add a menu with some items, some separators, and a sub-menu. 
    DocumentApp.getUi().createMenu('Utilities') 
     .addItem('Insert Date (dd MMMM yyyy)', 'insertAtCursor') 
     .addToUi(); 
} 

function insertAtCursor() { 
    InsertAtCursor.insertAtCursor() 
} 
: 당신이 insertAtCursor 기능이 라이브러리를 사용하여 문서의 컨텍스트에서 호출 될 때, 메뉴를 생성하고 또한 단지 라이브러리를 호출하는 함수를 작성하는 데 필요한 라이브러리를 사용하는 스크립트에서
+0

재미 방법 문제를 작성하는 것은 생각을 흔들고 ... 내가 다른 문서 스크립트에서 함수를 작성하는 경우 , 작동합니다. 'function insertAtCursor() {InsertDate.insertAtCursor}' 그러나 이것은 여전히 ​​내가하고 싶은 것 (또는 다른 사람들이하는 것)보다 더 많은 작업처럼 보입니다. 이를위한 더 나은 방법에 대한 제안은 환영합니다. –

답변

0

다음 비트 깨끗하고 무슨 일이 일어나고로 명확하지 않다 작동합니다 :

function onOpen() { 
    InsertAtCursor.onOpen() 
} 

function insertAtCursor() { 
    InsertAtCursor.insertAtCursor() 
}