LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Monday, August 15, 2022

Google SpreadsheetのnotesをDocumentに書き出したい


Google SpreadsheetのnotesをまとめてDocumentに書き出したくて書いたコードです。

実行プロセス
  1. getSheetNotes():noteを取得したいシートの範囲を指定して取得します
  2. notes.join("\n"):取得したデータ(配列)を改行でつないで文字列にします
  3. createSingleSentence(str):一文ごとに分けます
    • str.replace(/(?<![a-zA-Z0-9!?])[\s ](?![a-zA-Z0-9!?])/g, ""):英数以外の不要な半角スペースを削除(もとの文章に不要な半角がなければ省略)
    • .replace(/。/g, "。\n\n"):すべての「。」のあとに改行2つ追加
  4. addTextToDocument(singleSentences):指定したドキュメントに書き出します



コード.gs
function writeNotesToDocument() {
  const notes = getSheetNotes();
  const str = notes.join("\n");
  const singleSentences = createSingleSentence(str);
  addTextToDocument(singleSentences);
}

function getSheetNotes() {
  const ssUrl = "SPREADSHEET_URL";
  const ss = SpreadsheetApp.openByUrl(ssUrl);
  const sh = ss.getSheets()[0];
  const col = "E";
  const startRow = 2;
  const endRow = 378;
  const range = sh.getRange(col + startRow + ":" + col + endRow);
  const notes = range.getNotes();
  Logger.log(notes);
  return notes;
}

function createSingleSentence(str) {
  const sigleSentence = str.replace(/(?<![a-zA-Z0-9!?])[\s ](?![a-zA-Z0-9!?])/g, "").replace(/。/g, "。\n\n");
  return sigleSentence;
}

function addTextToDocument(text) {
  const url = "DOCUMENT_URL";
  const doc = DocumentApp.openByUrl(url);
  const body = doc.getBody();
  body.appendParagraph(text);
}


参考

スプレッドシートのノートを取得する

Googleドキュメントにテキストを追加したい

正規表現で英数以外のテキストの間にある半角スペースを削除したい

Google Driveで特定フォルダ内にあるファイルの説明をDocumentに書き出したい


Google Driveの特定フォルダ内にあるファイルの説明をすべて取得してドキュメントに書き出したくて書いたコードです。

書き出す順番はファイル名を昇順で並べ替えています。

実行プロセスの概要
  1. getFileDescriptionsInFolder() :フォルダを指定してファイルをすべて取得します
  2. fileObjArray.sort(asc):ファイル名の昇順で並べ替えます
  3. createDescriptionArray(sortedArray) :説明だけの配列を作ります
  4. descriptionsArray.join("\n") :配列を改行でつなげた文字列にします
  5. addTextToDocument(str) :指定したDocumentに書き出します



コード.gs
function addFileDescriptionsToDocument() {
  const fileObjArray = getFileDescriptionsInFolder();
  const sortedArray = fileObjArray.sort(asc);
  Logger.log(sortedArray);
  const descriptionsArray = createDescriptionArray(sortedArray);
  const str = descriptionsArray.join("\n");
  addTextToDocument(str);
}

function getFileDescriptionsInFolder() {
  const folderId = "FOLDER_ID";
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFiles();
  let array = [];
  while(files.hasNext()) {
    const file = files.next();
    const obj = {};
    obj["name"] = file.getName();
    obj["url"] = file.getUrl();
    obj["description"] = file.getDescription();
    array.push(obj);
  }
  return array;
}

function asc(a, b){
  const targetA = a.name;
  const targetB = b.name;
  if(targetA > targetB){
    return 1;
  }else if(targetA < targetB ){
    return -1;
  }else{
   return 0;
  }
}

function createDescriptionArray(sortedArray) {
  const array = [];
  const target = "description";
  for(let i = 0; i < sortedArray.length; i++){
    const description = sortedArray[i][target];
    array.push(description);
  }
  return array;
}

function addTextToDocument(description) {
  const url = "DOCUMENT_URL";
  const doc = DocumentApp.openByUrl(url);
  const body = doc.getBody();
  body.appendParagraph(description);
}



参考

Google Driveのファイルの説明をフォルダ単位で取得したい

Google Driveのファイルの説明をフォルダ単位で取得したい


ファイル単位で説明を取得する方法は
で書きました。

ここではフォルダ単位でファイルの説明を取得してみます。



コード.gs
function getFileDescriptionsInFolder() {
  const folderId = "FOLDER_ID";
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFiles();
  let array = [];
  while(files.hasNext()) {
    const file = files.next();
    const obj = {};
    obj["name"] = file.getName();
    obj["url"] = file.getUrl();
    obj["description"] = file.getDescription();
    array.push(obj);
  }
  Logger.log(array);
}


実行すると以下のようなログが出力されます

[
  {
    name=ファイル名, 
    description=説明, 
    url=ファイルのURL
  }, 
  {
    name=ファイル名, 
    description=説明, 
    url=ファイルのURL
  }, 
]


参考

Google Driveにあるファイルの説明を読み書きしたい 

Google Driveにあるファイルの説明を読み書きしたい


ここで言うGoogle Driveにあるファイルの説明とは

ファイルの詳細にある「説明を追加」項目です。
詳細の表示・非表示は右上の「i」アイコンで切り替えられます。



説明を追加する



コード.gs
function updateFileDescription() {
  const fileId = "FILE_ID;
  const text = "テキスト入力\n次の行";
  const file = DriveApp.getFileById(fileId);
  file.setDescription(text);
}



実行結果





説明を取得する



コード.gs
function getFileDescription() {
  const fileId = "FILE_ID";
  const file = DriveApp.getFileById(fileId);
  const description = file.getDescription();
  Logger.log(description);
}



実行結果



参考

getDescription() 

setDescription(description)

Latest post

スプレッドシートA列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...