2017-08-10 9 views
0

私は定期的に特定のウェブサイトからのすべてのリンクをGoogleドキュメントファイルに保存する必要があります。私はそれをしようと数時間を費やしたが、私は初心者であり、何も私のために働くものではない。私は何か提案を感謝します。ここでGoogle Apps Script - すべてのリンクをウェブサイトからGoogleドキュメントファイルに保存するにはどうすればよいですか?

は私の試みの一つである(しかし、それはとにかく動作しないとして、それを無視することはおそらく良いです):Google AppsのスクリプトはJavaScriptをベースのスクリプト言語であることを

function save_links() { 
    // create a google doc file named 'links' 
    var doc = DocumentApp.create('links'); 

    // save the source code of the website in question to a string 
    var str = UrlFetchApp.fetch('https://www.the_website_in_question').getContentText(); 

    // find all links 
    var link = str.findText('https:\/\/.*\/'); 

    // save every link to the google doc file 
    while (link != null) { 

    var foundLink = link.getElement().asText(); 
    doc.getBody().appendParagraph(foundLink);  
    link = link.findText('http:\/\/.*\/', link); 

    } 
} 

答えて

2

注意。 適切な正規表現を使用する必要があります。

function save_links() { 
    // create a google doc file named 'links' 
    var doc = DocumentApp.create('links'); 

    // save the source code of the website in question to a string 
    var str = UrlFetchApp.fetch('https://riyafa.wordpress.com/').getContentText(); 

    var regExp=/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm; 
    var theResult= str.match(regExp); 

    // save every link to the google doc file 
    for(i in theResult){ 
    doc.getBody().appendParagraph(theResult[i]); 

    } 
} 
+0

ありがとうございました!あなたのメッセージは分かりませんが、ソースコードは魅力的に機能しました! – Jane

+0

学ぶjavascriptはメッセージだった:) –

関連する問題