2017-01-15 6 views
0

私はC#で生成できる2000語のドキュメントを持っていますが、それらをネイティブのGoogleドキュメント形式でGoogleドキュメントにアップロードする必要があります。ワードドキュメントからネイティブgoogleドキュメントへの変換

私はThis guideを見て変換しようとしましたが、コードは最後の2行目で失敗します。

(以下マイコード)

function myFunction() { 
    var folder = DriveApp.getFoldersByName("test").next(); 
    var contents = folder.getFiles(); 
    while (contents.hasNext()){ 
    var file = contents.next(); 
    var fileName = file.getName(); 
    var officeFile = DriveApp.getFilesByName(fileName).next(); 
    // Use the Advanced Drive API to upload the Excel file to Drive 
    // convert = true will convert the file to the corresponding Google Docs format 
    var uploadFile = JSON.parse(UrlFetchApp.fetch(
     "https://www.googleapis.com/upload/drive/v2/files?uploadType=media&convert=true", 
     { 
     method: "POST", 
     contentType: officeFile.getMimeType(), 
     payload: officeFile.getBlob().getBytes(), 
     headers: { 
      "Authorization" : "Bearer " + ScriptApp.getOAuthToken() 
     }, 
     muteHttpExceptions: true 
     } 
    ).getContentText()); 


    // Remove the file extension from the original file name 
    var fileName2 = officeFile.getName(); 
    fileName2 = fileName2.substr(0, fileName2.lastIndexOf(".")); 


    // Update the name of the Google Sheet created from the Excel sheet 
    DriveApp.getFileById(uploadFile.getID()).setName(fileName2); // FAILS HERE 

    //Logger.log(uploadFile.alternateLink); 
    }} 

はTypeError:オブジェクトの[オブジェクトのオブジェクト]関数のgetIdを見つけることができません。 (行33、ファイル "コード")

これはどのような種類のエラーであるかを理解していますが、解決方法は必ずしもわかりません。

+0

可能性があり、IdはuploadFile.id' 'として得られる - それは財産ですアップロードAPIによって返されるオブジェクトの – FTP

+0

@ azq私はそれを試みたが、同じエラーが発生していた。私はもう一度やり直します。 –

+0

オブジェクトuploadFileをログに記録して、どのプロパティがあるかを確認してください。 'Logger.log(JSON.stringify(uploadFile))'などです。 – FTP

答えて

1

UrlFetchAppの操作は必要ありません。 Advanced Drive Serviceは、copyメソッドの一部としての変換を提供します。

var fileId = 'ID_of_Word_file'; 
    Drive.Files.copy({}, fileId, {'convert': true}); 

このサービスは、scroptメニューで有効にする必要があります:リソース>高度なサービスを参照してください。

フォルダを反復処理する場合、ファイルイテレータから各ファイルを.next()で取得し、getId()を使用してそのIDを取得します。上記のように進んでください。

最初の引数に空のオブジェクトは、新しいファイルに名前を付けるために使用することができます。例えば、それはあなたが引用されたガイドで{'title': 'Name of new file'}

+0

ファイルを同じディレクトリにコピーします。ありがとうございました! –

関連する問題