使用脚本添加='Sheet1!'D3公式,该公式具有在脚本内提取的工作表名称

时间:2018-07-27 14:23:40

标签: google-sheets formulas

我有这个:

var app = SpreadsheetApp;
var activesheet = app.getActiveSpreadsheet().getActiveSheet();
var RecipeList = app.getActiveSpreadsheet().getSheetByName('Recipe List')

var recipename = activesheet.getRange('D3').getValue();
var costperserve = activesheet.getRange('J5').getValue();
var foodcostpercentage = activesheet.getRange('J6').getValue();
var sellingprice = activesheet.getRange('J7').getValue();
var grossprofit = activesheet.getRange('J10').getValue();
var spreadsheetname = activesheet.getSheetName() ;
var Last = RecipeList.getLastRow();

RecipeList.getRange(Last+1,1,1,1).setValue(recipename), {contentsOnly:true};
RecipeList.getRange(Last+1,2,1,1).setValue(costperserve), {contentsOnly:true};  
RecipeList.getRange(Last+1,3,1,1).setValue(foodcostpercentage), 
{contentsOnly:true};  
RecipeList.getRange(Last+1,4,1,1).setValue(sellingprice), {contentsOnly:true};
RecipeList.getRange(Last+1,5,1,1).setValue(grossprofit), {contentsOnly:true};
RecipeList.getRange(Last+1,6,1,1).setValue(spreadsheetname), 
{contentsOnly:true};

尽管我希望将值= ( = 'spreadsheetname' !D3)作为仅值的姿势,以便我可以更改值并对其进行更新。

1 个答案:

答案 0 :(得分:0)

有一种setFormula()方法可以像setValue()一样使用,但这不是必需的。您也可以在这里使用setValue()

您似乎正在尝试将{contentsOnly:true}的选项传递给setValue(),但这是无效的。如您在链接文档中所见,此方法不允许任何选项。而且,它不允许使用该特定选项,因为它只会影响内容。

尝试以下示例代码:

function insertFormulaTest() {
  var ss = SpreadsheetApp.getActive();
  var sourceSheet = ss.getSheetByName("Source Sheet");
  var sourceCell = sourceSheet.getRange("D3");
  var formula = "='" + sourceSheet.getName() + "'!" + sourceCell.getA1Notation(); // "='Source Sheet'!D3"
  var destinationSheet = ss.getSheetByName("Destination Sheet");
  destinationSheet.getRange("A1").setValue(formula);
}