LoginSignup
HARUNA3
@HARUNA3

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

GAS Unexpected error while getting the method or property getFolderById on object DriveApp.というエラー

Discussion

行いたい事

google formから取得したデータを元に、見積書を作成しPDF化、PDFをメールに添付し下書き状態で保存するプログラムをGASで作成したい。

解決したいこと

PDFを保存する際のgoogle driveのフォルダを指定したいが、エラーが出てしまうため解消したい

発生している問題・エラー

Exception: Unexpected error while getting the method or property getFolderById on object DriveApp.

該当するソースコード

const mailaddress = "hoge@gmail.com"
const ss_id = "1dDyO-hoge-8Wni5Ewg"
const sh_name = "自動見積り(回答)"

function GetDataByForm() {
  const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const lastRow = sh.getLastRow();

  const container = sh.getRange(lastRow, 14).getValue();
  const option = sh.getRange(lastRow, 15).getValue();
  const company = sh.getRange(lastRow, 16).getValue();
  const name = sh.getRange(lastRow, 17).getValue();
  const mail = sh.getRange(lastRow, 18).getValue();
  const phoneNumber = sh.getRange(lastRow, 19).getValue();

  const nmncapacity = sh.getRange(lastRow, 2).getValue();
  const nmnlots = sh.getRange(lastRow, 3).getValue();
  const nmncapacity2 = sh.getRange(lastRow, 4).getValue();
  const nmnlots2 = sh.getRange(lastRow, 5).getValue();

  // ここまででシートの内容は全て読み取れている
}

// シート内容を見積書に転載
function CreateQuotationSheet() {
  const templateID = "見積書のベースになるシートのIDを入れております";
  const templateSS = DriveApp.getFileById(templateID);
  const folderID = "見積書ストックと題名をつけた空のフォルダのIDを入れております";
  const folder = DriveApp.getFolderById(folderID)
}

// 見積書をPDF化
// PDFファイルをフォルダに保存
// 新規GMAILを作成 
  // PDFファイルをメールに添付
  // 下書き保存状態でアラートが飛ぶようにする

自分で試したこと

  • GAS referenceを参考に、getFolderByIdの使い方の確認
  • folder IDの取得方法の確認 
https://drive.google.com/drive/folders/アルファベット列

→上記のURLからアルファベット列をコピペしているので、IDに問題は無いと思います

  • 元々下記のコードを参考にして書いておりましたが、挙動を1つずつ確認しながら進めるために一度削除し、google formから送られてきたデータが『function GetDataByForm』 の関数できちんと取得出来ている事を確認し、関数を分けてみました。
参考コード
//新規スプレッドシートのファイル名を生成する処理
  var date = new Date();
  var filename = Utilities.formatDate(date, "JST", "YYYYMMDDHHmm_見積書");

  //請求書を原本から複製する処理
  var folder = DriveApp.getFolderById(FolderId);
  var SSQuote = DriveApp.getFileById(QuoteStId); // コピー元のファイルIDを取得&入力
  var newfile = SSQuote.makeCopy(filename, folder);
  var idNewfile = newfile.getId();
  var SSQuote = SpreadsheetApp.openById(idNewfile);
  var stQuote = SSQuote.getSheetByName("見積書")
  stId = stQuote.getSheetId();
0

こんにちは。
(普段はSpreadSheetAppの方を使っているので、DriveAppの方は的外れな解答をしてしまうかもしれません。ご容赦ください。)

IDが確実に正しいとなると、アクセス権限がうまく設定されていない可能性はどうでしょうか?

またIDについては、?id=などを含んだ形になっていないかはどうでしょう?

0Like

Your answer might help someone💌