在Google Apps脚本中将制表符转换为空格 [英] Tab characters converted to spaces in Google Apps Script

查看:91
本文介绍了在Google Apps脚本中将制表符转换为空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我撰写的Google Apps脚本将多个报告合并为制表符分隔(TSV)格式。但是,我注意到,每当我写入文件时,我的字符串中的选项卡都将被转换为空格。是否还有其他的字符串文字我应该使用?

  var content = 
data.parameters.time [0] +\ t+
data.parameters.url [0] +\ t+
data.parameters.code_name [0] +\ t+
data .parameters.app_name [0] +\ t+
data.parameters.version [0] +\ t+
data.parameters.cookies [0] +\t +
data.parameters.lang [0] +\ t+
data.parameters.platform [0] +\ t+
data.parameters.user_agent [ 0] +\ t+
data.parameters.ex [0];
fld.createFile(makeid()+.tab,content,ContentService.MimeType.CSV);


解决方案

使用 \ t 字符串字面值 - 而不是您在 MimeType enum。 google.com/apps-script/reference/content/mime-typerel =nofollow> ContentService 。 DriveApp方法期望 MimeType 作为一个字符串。尝试:


$ b

  fld.createFile(makeid()+ .tab,content,MimeType.CSV); 

下面的代码片断显示了当MimeType设置正确时, >

  function myFunction(){
var content = $ b $'time'+ \ t+
'url'+\t+
'code_name'+\t+
'etcetera';
Logger.log(Initial TSV:+ JSON.stringify(content.split(\ t)));

var newFile = DriveApp.createFile(Delete-me+.tab,content,MimeType.CSV);

//通过读回文件
来检查我们的结果var blob = newFile.getBlob();
var docContent = blob.getDataAsString()
Logger.log(Result TSV:+ JSON.stringify(docContent.split(\t)));
}

记录:

  [15-04-07 14:53:08:767 EDT]初始TSV:[time,url,code_name,etcetera ] 
[15-04-07 14:53:09:542 EDT]结果TSV:[time,url,code_name,etcetera]


I have a Google Apps Script I am writing that combines a number of reports into a consolidated tab-delimited (TSV) format. However, I am noticing that whenever I write to file, the tabs in my strings are being converted into spaces. Is there some other string literal that I should use?

    var content = 
        data.parameters.time[0] + "\t" +
        data.parameters.url[0] + "\t" +
        data.parameters.code_name[0] + "\t" +
        data.parameters.app_name[0] + "\t" +
        data.parameters.version[0] + "\t" +
        data.parameters.cookies[0] + "\t" +
        data.parameters.lang[0] + "\t" +
        data.parameters.platform[0] + "\t" +
        data.parameters.user_agent[0] + "\t" +
        data.parameters.ex[0];
    fld.createFile(makeid() + ".tab", content, ContentService.MimeType.CSV);

解决方案

Nothing is wrong with using the \t string literal - rather you're using the wrong MimeType enum, from the ContentService. DriveApp methods expect MimeType as a string. Try:

fld.createFile(makeid() + ".tab", content, MimeType.CSV);

Here's a snippet that shows the tab characters survive the file write when the MimeType is properly set:

function myFunction() {
  var content = 
        'time' + "\t" +
        'url' + "\t" +
        'code_name' + "\t" +
        'etcetera';
  Logger.log("Initial TSV: " + JSON.stringify(content.split("\t")));

  var newFile = DriveApp.createFile("Delete-me" + ".tab", content, MimeType.CSV);

  // Check our result, by reading back the file
  var blob = newFile.getBlob();
  var docContent = blob.getDataAsString()
  Logger.log("Result TSV: " + JSON.stringify(docContent.split("\t")));
}

Logs:

[15-04-07 14:53:08:767 EDT] Initial TSV: ["time","url","code_name","etcetera"]
[15-04-07 14:53:09:542 EDT] Result TSV: ["time","url","code_name","etcetera"]

这篇关于在Google Apps脚本中将制表符转换为空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆