【问题标题】:Google App Script Unexpected error while getting the method or property getFileById on object DriveAppGoogle App Script 在对象 DriveApp 上获取方法或属性 getFileById 时出现意外错误
【发布时间】:2021-05-20 02:32:47
【问题描述】:

我有此代码可将我的 Google 工作表导出到“CSV”分隔标签“|”感谢@iansedano 的帮助。

这就是脚本:

function createCsvContent(values, delimiter) {

  // This converts the values 2D array into a simple array of strings delimited by the delimiter
  const stringArray = values.map(row => row.join(delimiter))
  // Then it joins all the strings with newlines
  const output = stringArray.join("\n")

  return output
}


function createCsv() {
  // Get all the values from the sheet as a 2D array
  const file = SpreadsheetApp.getActive();
  const sheet = file.getSheetByName("LIST");
  const range = sheet.getDataRange();
  const values = range.getValues();

  // call the function to create the csv-like content
  const csvContent = createCsvContent(values, "|")

  // create the file in drive
  //DriveApp.createFile('csvFile', csvContent, MimeType.PLAIN_TEXT)
  DriveApp.getFileById("[10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y]").setContent(csvContent)
}

所以每次运行脚本时,我都想覆盖这个文件https://docs.google.com/document/d/10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y/edit

我无法提交正确的文件 ID。如果我没记错的话,这不是文件 ID '10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y'。

我在哪里可以获得 Google Doc 的真实 ID?

【问题讨论】:

    标签: google-apps-script google-sheets google-cloud-platform


    【解决方案1】:

    如果您使用[10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y]的值作为文件ID,请将其修改为10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3YRef所以,当你的脚本被修改后,变成如下。

    发件人:

    DriveApp.getFileById("[10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y]").setContent(csvContent)
    

    收件人:

    DriveApp.getFileById("10eI7WAO688nQHRqj7ymscN5I5fJpP4fVTUn_qZMuq3Y").setContent(csvContent);
    

    注意:

    • 如果要直接使用https://docs.google.com/document/d/###/edit的URL,也可以使用如下脚本。

        DocumentApp.openByUrl("https://docs.google.com/document/d/###/edit").getBody().setText(csvContent);
      

    参考:

    【讨论】:

    • @Simon GIS 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多