【问题标题】:How can I create a google spreadsheet from a template?如何从模板创建谷歌电子表格?
【发布时间】:2016-12-05 18:42:25
【问题描述】:

我有一个使用 Google Drive 和 Spreadsheets API 的网站。如果没有找到具有特定条件的电子表格,我想要做的是在用户的 Google Drive 上创建一个电子表格。 该电子表格应该是我的 Google 云端硬盘上公开电子表格的副本。

/**
* Find the right file.
*/
function findFile() {
  appendPre('Finding file…')
  var request = gapi.client.drive.files.list({
    "q": "fullText contains 'Omregning fra klokketimer (60 minutter) til skoletimer (45 minutter) (Summering av fravær i Skolearena er i klokketimer)'"
  });
  request.execute(function(resp) {
    if (!resp.error) {
      console.log(resp);
      spreadSheetId = getID(resp.items);
    } else if (resp.error.code == 401) {
      // Access token might have expired.
      checkAuth();
    } else {
      appendPre('An error occured: ' + resp.error.message);
    }
  });
}

/**
* Find ID of the right file or create a file if none exist and return ID
*/
function getID(items) {
  for (var i = 0; i < items.length; i++) {
    if (items[i].mimeType == "application/vnd.google-apps.spreadsheet") {
      return items[i].id;
    }
  }
  // Code to create spreadsheet from public spreadsheet here
}

我已尝试查看 API 文档并使用 google 进行搜索,但我没有找到任何有关如何执行此操作的信息。我真的希望这是可能的。

【问题讨论】:

标签: javascript google-sheets google-drive-api


【解决方案1】:

解决方案实际上比我想象的要容易:

function copyFile(id, name) {
  var body = {'title': name};
  var request = gapi.client.drive.files.copy({
    'fileId': id,
    'resource': body
  });
  request.execute(function(resp) {
    console.log('Copy ID: ' + resp.id);
    return resp.id;
  });
}

【讨论】:

    猜你喜欢
    • 2017-04-28
    • 2021-04-22
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多