0

これが私のコードです。基本的に、私はオブジェクトを作成しています。その属性の一部は、オブジェクトをインスタンス化したときに実行する必要のある関数によって設定されます。私はこれをGoogleScriptsコードエディタで開発しており、バグなしで実行されますが、実際には何もしません。

この関数は、タイムトリガーによって定期的に呼び出されます

function addFoldersToSites(){

  //DriveObject class, has a folder id and page url, and uses those to get attachments
  function DriveObject(folder_id,page_url) {
    this.folder_id = folder_id
    this.page_url = page_url
    this.files = setFiles
    this.page = setPage
    this.attachments = setAttachments
  }
  function setFiles(){return DocsList.getFolderById(this.folder_id).getFiles();}
  function setPage(){return SitesApp.getPageByUrl(this.page_url);}
  function setAttachments(){return this.page.getAttachments();}


  //instantiate drive objects
  //if you want to add drive things to new site pages, this is where you do it
  //*************************************************************************
  var engpage = new DriveObject('0B6esS6X9k9LvWWFfc1oyN0VfeTg','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
  var productpage = new DriveObject('0B6esS6X9k9LveUI0dmxxLWplaTA','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
  var adminpage= new DriveObject('0B6esS6X9k9LvVUVDUlBWWWhJcDg','https://sites.google.com/a/drawbrid.ge/resources/home/files');
  var genonboarding = new DriveObject('0B6esS6X9k9LvbjRtanE4eGNkdlE','https://sites.google.com/a/drawbrid.ge/resources/home/general-onboarding');
  var salespage= new DriveObject('0B6esS6X9k9LvQ2JJN0pCblNyME0','https://sites.google.com/a/drawbrid.ge/resources/sales/files');
  var researchpage = new DriveObject('0B6esS6X9k9LvM1RKeHVXRkNBZ1k','https://sites.google.com/a/drawbrid.ge/resources/market-research');

  //function that iterates through folder files and puts them in the google site frame
  function showFolderInSite(attachments, page, files) {
    var attachments = attachments
    var page = page
    var files = files

    for (i in attachments) {
      attachments[i].deleteAttachment();
    }

    for (i in files) {
      page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
    }
  }

  //run functions
  showFolderInSite(engpage.attachments,engpage.page,engpage.files)
  showFolderInSite(productpage.attachments,productpage.page,productpage.files)
  showFolderInSite(adminpage.attachments,adminpage.page,adminpage.files)
  showFolderInSite(genonboarding.attachments,genonboarding.page,genonboarding.files)
  showFolderInSite(salespage.attachments,salespage.page,salespage.files)
  showFolderInSite(researchpage.attachments,researchpage.page,researchpage.files)
}
4

2 に答える 2

2

すべての属性を一度に設定してオブジェクトをインスタンス化する唯一のアトミックな方法は、それらすべてをコンストラクター関数に渡すことです。

次に、コンストラクターはこれらの引数を使用して、適切な初期化の一部としてそれらの引数を処理する必要がある可能性のある他の関数を呼び出すことができます。

したがって、基本的に次の 2 つの選択肢があります (最初の選択肢はよりアトミックな初期化子です)。

function myConstructor(a, b, c, d, e, f) {
    this.whatever = a;
    this.aabb = processArg(b, c);
    // etc...
}

var myObj = new myConstructor(true, 3, "foo", 4, 5, 6);

またはこれ:

function myConstructor() {
}
myConstructor.prototoype = {
    init1: function(a, b) {},
    init2: function(a, b) {},
    init3: function(a, b) {},
}

var myObj = new myConstructor();
myObj.init1(true, 3);
myObj.init2("foo", 4);
myObj.init3(5, 6);

最初のオプションはよりアトミックであり、適切な初期化呼び出しをすべて行う呼び出し元の負担が少なくなりますが、オブジェクトを初期化する方法が複数ある場合は、2 番目のオプションの方が柔軟です。私は前者を好みます。一般的には、発信者が台無しになる方法を少なくする方がよいからですが、場合によっては、2 番目のオプションの柔軟性が役立つこともあります。

于 2013-01-11T18:15:41.857 に答える
1

これがあなたが探しているものだと思います(DocsListとSitesAppにアクセスできることも確認してください):

function addFoldersToSites(){

        //DriveObject class, has a folder id and page url, and uses those to get attachments
        function DriveObject(folder_id,page_url) {
            this.folder_id = folder_id;
            this.page_url = page_url;
            this.showFolderInSite();
        }
        DriveObject.prototype = {};
        DriveObject.prototype.constructor = DriveObject;
        DriveObject.prototype.files = function setFiles(){return DocsList.getFolderById(this.folder_id).getFiles();}
        DriveObject.prototype.page = function setPage(){return SitesApp.getPageByUrl(this.page_url);}
        DriveObject.prototype.attachments = function setAttachments(){return this.page.getAttachments();}
    //function that iterates through folder files and puts them in the google site frame
        DriveObject.prototype.showFolderInSite = function showFolderInSite() {
            var attachments = this.attachments();
            var page = this.page();
            var files = this.files();

            for (i in attachments) {
                attachments[i].deleteAttachment();
            }

            for (i in files) {
                page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
            }
        }

        //instantiate drive objects
        //if you want to add drive things to new site pages, this is where you do it
        //*************************************************************************
        var engpage = new DriveObject('0B6esS6X9k9LvWWFfc1oyN0VfeTg','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
        var productpage = new DriveObject('0B6esS6X9k9LveUI0dmxxLWplaTA','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
        var adminpage= new DriveObject('0B6esS6X9k9LvVUVDUlBWWWhJcDg','https://sites.google.com/a/drawbrid.ge/resources/home/files');
        var genonboarding = new DriveObject('0B6esS6X9k9LvbjRtanE4eGNkdlE','https://sites.google.com/a/drawbrid.ge/resources/home/general-onboarding');
        var salespage= new DriveObject('0B6esS6X9k9LvQ2JJN0pCblNyME0','https://sites.google.com/a/drawbrid.ge/resources/sales/files');
        var researchpage = new DriveObject('0B6esS6X9k9LvM1RKeHVXRkNBZ1k','https://sites.google.com/a/drawbrid.ge/resources/market-research');
    }
于 2013-01-11T19:54:04.753 に答える