使用 Google Sheets API 阻止“+”和其他符号生成公式

Stop a '+' and other symbols from generating a formula with Google Sheets API

这些单元格文本 = thinking+ thinking=+ thinking 显示错误。 我找到了这些解决方法:

  1. +.
  2. 之前输入撇号 ' 作为第一个字符
  3. 以字符串公式形式输入内容,如 ="+5 blah"

但是,我需要使用 Google 表格 API 来完成此操作。我需要保持单元格的所有内容不变。 可能吗?

格式 > 数字 > 纯文本

如果您从脚本本身获取数据,例如从 csv 导入,您可以在开头附加一个单引号以按原样显示。

function forceString() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var txt = `="THINKING"`;
  sheet.getActiveCell().setValue(`'`+txt);
}

valueInputOption='RAW' 修复了问题。

第一个插入文本不变,第二个插入公式:

api.spreadsheets().values().update(
    spreadsheetId=ssId, range='A1', body={'values': text},
    valueInputOption='RAW').execute()

api.spreadsheets().values().update(
    spreadsheetId=ssId, range='B1', body={'values': [[formula]]},
    valueInputOption='USER_ENTERED').execute()