質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google スプレッドシート

Google スプレッドシートは、フリーで利用できる表計算ソフト。Webアプリのためインターネットに接続することで利用できます。チャートやグラフの作成のほか、シートを他のユーザーと共有したり、同時に作業を進めることも可能です。

Google Apps Script

Google Apps ScriptはGoogleの製品と第三者のサービスでタスクを自動化するためのJavaScriptのクラウドのスクリプト言語です。

Q&A

解決済

1回答

1529閲覧

gasでXMLをXmlServiceしてスプレッドシートに保存したい。

BBA

総合スコア60

Google スプレッドシート

Google スプレッドシートは、フリーで利用できる表計算ソフト。Webアプリのためインターネットに接続することで利用できます。チャートやグラフの作成のほか、シートを他のユーザーと共有したり、同時に作業を進めることも可能です。

Google Apps Script

Google Apps ScriptはGoogleの製品と第三者のサービスでタスクを自動化するためのJavaScriptのクラウドのスクリプト言語です。

0グッド

0クリップ

投稿2022/11/02 05:17

XMLのスクレイピングをGASでしてます

XmlからXmlServiceを使用して取得してスプレッドシートに保存できるコードを書いています

保存はできましたが重複した最後のテキストだけ保存され困っています。

XMLのスクレイピングをスプレッドシートに保存したい

最終はスプレッドシートにXMLデータを保存
することです。
下記の表のようにするのが目標です

ArticleCaptionArticleTitleItemTitleSentence
(業務)第三条司法書士は、この法律の定めるところにより、他人の依頼を受けて、次に掲げる事務を行うことを業とする。
登記又は供託に関する手続について代理すること。

*ItemTitleなど、一号から数号まであります。

発生している問題

itemsentenceの部分などeachでループしたテキストが最後だけ保存されてしまいます。

該当のソースコード

function mFunction() { let url = 'https://elaws.e-gov.go.jp/api/1/lawdata/昭和二十五年法律第百九十七号'; let xml = UrlFetchApp.fetch(url).getContentText(); let document = XmlService.parse(xml); let root = document.getRootElement(); const applData = root.getChild("ApplData"); const lawFullText = applData.getChild("LawFullText"); const law = lawFullText.getChild("Law"); const lawBody = law.getChild("LawBody"); const mainProvision = lawBody.getChild("MainProvision"); const chapter = mainProvision.getChild("Chapter"); const articles = chapter.getChildren("Article"); articles.forEach(function(article){ var spreadsheet = SpreadsheetApp.openById('***************'); var sheet = spreadsheet.getSheetByName('*****'); var lastrow = sheet.getLastRow(); var recordrow = lastrow + 1; const articleCaption = article.getChild("ArticleCaption"); const articleTitle = article.getChild("ArticleTitle"); const paragraphs = article.getChildren("Paragraph"); sheet.getRange("A" + recordrow).setValue(articleCaption.getText()); sheet.getRange("B" + recordrow).setValue(articleTitle.getText());    if (paragraphs != null){ paragraphs.forEach(function(paragraph){ const paragraphNum = paragraph.getChild("ParagraphNum"); const paragraphSentence = paragraph.getChild("ParagraphSentence"); sheet.getRange("C" + recordrow).setValue(paragraphNum.getValue()); sheet.getRange("F" + recordrow).setValue(paragraphSentence.getValue()); const items = paragraph.getChildren("Item"); if (items !== null) { items.forEach(function (item) { const itemtitle = item.getChild("ItemTitle"); sheet.getRange("D" + recordrow).setValue(itemtitle.getText()); const itemSentence = item.getChild("ItemSentence");     sheet.getRange("F" + recordrow).setValue(itemSentence.getValue()); const columns = itemSentence.getChildren("Column"); if (columns !== null) { columns.forEach(function (column) {      sheet.getRange("F" + recordrow).setValue(column.getText()); }) } const subitem1s = item.getChildren("Subitem1"); if (subitem1s !== null) { subitem1s.forEach(function (subitem1) { const subitem1Title = subitem1.getChild("Subitem1Title");     sheet.getRange("D" + recordrow).setValue(subitem1Title.getText()); const subitem1Sentences = subitem1.getChildren("Subitem1Sentence"); subitem1Sentences.forEach(function (subitem1Sentence) {      sheet.getRange("F" + recordrow).setValue(subitem1Sentence.getValue()); }); const subitem2s = subitem1.getChildren("Subitem2"); if (subitem2s !== null) { subitem2s.forEach(function (subitem2) { const subitem2Title = subitem2.getChild("Subitem2Title"); sheet.getRange("D" + recordrow).setValue(subitem2Title.getText()); const subitem2Sentences = subitem2.getChildren("Subitem2Sentence"); subitem2Sentences.forEach(function (subitem2Sentence) { sheet.getRange("F" + recordrow).setValue(subitem2Sentence.getValue()); }); const subitem3s = subitem2.getChildren("Subitem3"); if (subitem3s !== null) { subitem3s.forEach(function (subitem3) { const subitem3Title = subitem3.getChild("Subitem3Title");         sheet.getRange("D" + recordrow).setValue(subitem3Title.getText()); const subitem3Sentences = subitem3.getChildren("Subitem3Sentence"); subitem3Sentences.forEach(function (subitem3Sentence) {       sheet.getRange("F" + recordrow).setValue(subitem3Sentence.getValue()); });    }) }    }) } }) } })} }) } }) }

試したこと

sentenceだけ取得してみましたが変わりませんでした。
consolelogでは全部取得できるのに
スプレッドシートに保存すると変わってしまいます。
解決法検索しましたが具体的に参考になりそうな方法が見つかりませんでした。

すみませんが解決のヒントなどお教えいただけないでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bebebe_

2022/11/02 06:19 編集

細かいとこまで把握していませんがセルに入力した後にrecordrowの値が変わっていないので同じセルに入力されているのではないでしょうか とりあえずitems.forEach(function (item)のループ内のF列に対してのsetValueがある次の行にrecordrow = recordrow + 1を追加してみたところ求めてる結果に近そうな形になっている気がします。 追記 35行目のsheet.getRange("F" + recordrow).setValue(paragraphSentence.getValue());の後にもrecordrow = recordrow + 1があれば目標に近くなりそうです
BBA

2022/11/05 11:39

遅くなりすみません。試してみます!
BBA

2022/11/06 05:37

重複してる部分も取れました!ありがとうございます!
guest

回答1

0

自己解決

質問にコメントくださった内容で解決できました!

投稿2022/11/06 05:37

BBA

総合スコア60

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問