简体   繁体   中英

App script keeps export csv file with double quotes

With this code, exported csv file has double quotes at each data.

How Can I export as CSV without double quotes??

function createDataUrl(type) {
  const mimeTypes = { csv: MimeType.CSV, pdf: MimeType.PDF };
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();


  var sheet_name = type;
  var file_name = ss.getSheetByName('설정').getRange('market').getValue() + sheet_name + '.csv';



  let url = null;
  url = `https://docs.google.com/spreadsheets/d/${ss.getId()}/gviz/tq?tqx=out:csv&gid=${ss.getSheetByName(sheet_name).getSheetId()}`;


  if (url) {
    const blob = UrlFetchApp.fetch(url, {
      headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
    }).getBlob();
    return {
      data:
        `data:${mimeTypes['csv']};base64,` +
        Utilities.base64Encode(blob.getBytes()),
      filename: file_name,
    };
  }
  return { data: null, filename: null };
}

In your script, how about the following modification?

From:

const blob = UrlFetchApp.fetch(url, {
  headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
}).getBlob();

To:

const res = UrlFetchApp.fetch(url, { headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` } });
const blob = Utilities.newBlob(res.getContentText().replace(/"/g, ""), MimeType.CSV);
  • By this modification, " is removed from the returned text data.

Note:

  • In this modification, it supposes that each value of the CSV data doesn't include " . Please be careful about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM