首页 > 解决方案 > Google Apps 脚本中的 makeCopy() 方法在我的驱动器而不是客户端驱动器中创建一个副本

问题描述

Google 表格插件应在客户端驱动器中创建文件的副本,而不是在原始驱动器中创建副本。

我的 Code.gs 文件中的以下脚本应该执行此操作,但似乎没有按预期工作:

function createACopy(id){
  var docName = DriveApp.getFileById(id).getName();
  incrementDownloadCount(id);
  return DriveApp.getFileById(id).makeCopy(docName).getUrl();
}

function incrementDownloadCount(id){
  var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(DATA_SHEET);
  var data = sheet.getRange(2, 1, sheet.getLastRow()-1, 1).getValues();
  var pos =data.map(function (obj) { return obj[0]; }).indexOf(id)
  if(pos > -1){
    var val = sheet.getRange("J"+(pos+2)).getValue()+1;
    sheet.getRange("J"+(pos+2)).setValue(val);
  }
}

这是 HTML 文件中的脚本:

    function makeACopy(){
    $("#templtDtl button").remove();
    $("#templtDtl").prepend(
            "<div id='copying'><img src='https://cdn.spreadsheet123.com/images/app/loading.gif' style='float:left;margin-right: 10px;'/>Making a copy...</div>"
    );
    google.script.run.withSuccessHandler(afterMakingCopy).createACopy(DETAIL[0]);
}

function afterMakingCopy(url){
    $("#copying").remove();
    $(document.createElement("div"))
            .html(
                    'File has been successfully copied to your Google Drive! Open the file by clicking on the button below, or check your Google Drive for the file named:<br/> "'+DETAIL[4]+'"<br/><br/>'+
                            "<a href='"+url+"' target='_blank'><button class='action' style='margin-left: 0px'>Open file</button></a>"
            )
            .prependTo($("#templtDtl"));
}

这是HTML:

<div id="section3" style="display:none;">
            <div style="margin-bottom:10px;">
                <button onClick="backButtonPressed(3)">◄ Back</button>
            </div>
            <div class="scrollable-wrapper" style="padding:10px 4px;height:320px;">
                <div id="templtTitle"></div>
                <div id="templtImg"><div id="templtThumb"></div></div>
                <div id="templtDtl" style="padding-left: 26px;"></div>
            </div>
        </div>

如果您需要任何其他代码,请告诉我?

谢谢

标签: google-apps-scriptgoogle-sheetsgoogle-drive-apigoogle-apps-script-addon

解决方案


推荐阅读