Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 使用google drive api复制工作表_Google Apps Script_Google Sheets_Google Drive Api - Fatal编程技术网

Google apps script 使用google drive api复制工作表

Google apps script 使用google drive api复制工作表,google-apps-script,google-sheets,google-drive-api,Google Apps Script,Google Sheets,Google Drive Api,我正在尝试使用google apps脚本复制/复制工作表。这样做可能吗?我在API文档中找不到此类调用的任何引用 对不起,据我所知,没有函数可以这样做。谷歌应用程序脚本可以做到这一点 我是用手工做的。我创建了一个新的工作表,并复制了公式/数据。但如果在公式中使用$,则会出现错误,读取工作正常,但写入是一件痛苦的事情,因为某些公式(例如带有$1引用的公式)不会写入工作表(因为存在错误)。所以你需要把它们换成A1。此外,您不能复制字体/颜色等 我这样做的代码是用Java编写的,但要将所需的代码分出来

我正在尝试使用google apps脚本复制/复制工作表。这样做可能吗?我在API文档中找不到此类调用的任何引用

对不起,据我所知,没有函数可以这样做。谷歌应用程序脚本可以做到这一点

我是用手工做的。我创建了一个新的工作表,并复制了公式/数据。但如果在公式中使用$,则会出现错误,读取工作正常,但写入是一件痛苦的事情,因为某些公式(例如带有$1引用的公式)不会写入工作表(因为存在错误)。所以你需要把它们换成A1。此外,您不能复制字体/颜色等


我这样做的代码是用Java编写的,但要将所需的代码分出来并不容易。

您可以使用部署为web应用程序的google应用程序脚本。 使用HTTP GET调用web应用程序以传输电子表格ID、要复制的工作表的名称和新工作表的名称。以下是我使用的脚本:

function doGet(e) {

  // get parameter from request (GET HTTP method)
  var spreadsheetID = e.parameter.spreadsheetID;
  var targetName = e.parameter.targetName;
  var sourceSheetName = e.parameter.sheetName;

  var response = copyWorksheet(spreadsheetID, sourceSheetName, targetName);

  return ContentService.createTextOutput(response);
}


function copyWorksheet(spreadsheetID, templateName, sheetName){

    // if there is a spreadsheet ID, find the spreadsheet with this ID
    if(typeof spreadsheetID == "string"){
      spreadsheet = SpreadsheetApp.openById(spreadsheetID);
    }else{
      return "failure; the spreadsheet " + spreadsheetID + " was not found";
    }

    // if there is a template name, find the worksheet with this name and set it as the active sheet;  (if there is no name provided, the active worksheet is duplicated)
    if(typeof templateName == "string"){
      template = spreadsheet.getSheetByName(templateName);
      if(template != null)
        spreadsheet.setActiveSheet(template);
    }

    // duplicate active sheet and set the new sheet as the active sheet
    newSheet = spreadsheet.duplicateActiveSheet();

  // rename the sheet
    if(typeof sheetName == "string"){
      sheetName.setName(sheetName);
    }

  return "success;" + sheetName + ";";
}
将此脚本附加到任何电子表格并部署它。使用以下命令调用它:

<scriptURL>?spreadsheetID=<spreadsheetID>&targetName=<targetName>&templateName=<sourceSheetName>
?电子表格ID=&targetName=&templateName=

因此,我将此脚本附加到一个工作表上,部署它,并使用获得的URL通过Python中的httplib2.Http()对象在电子表格上发出请求,我通过SignedJWTAssertionCredentials使用正确的Google驱动器ID、作用域、键等进行授权。但即使在检查/试验名称和ID之后,我也得到了404。你知道为什么会发生这种情况吗?